Declaring and Referencing Variables

Variables are similar to constants in that when you reference a variable's name in code, Visual C# .NET substitutes the variable's value in place of the variable name when the code executes. This doesn't happen at compile time, though. Instead, it happens at runtime—the moment the variable is referenced. This is because variables, unlike constants, can have their values changed at any time.

Declaring Variables

The act of defining a variable is called declaring. (Variables with scope other than local are dimensioned in a slightly different way, as discussed in the section on scope.) You've already defined variables in previous hours, so a declaration statement should look familiar to you:

							datatype
							variablename ...

Get Sams Teach Yourself Microsoft® Visual C#™ .NET in 24 Hours now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.