Assignment

Besides declaration of local variables, there’s also the need to initialize them before they can be used. C# is very strict about not permitting the use of uninitialized variables because that’s a very common source of errors found in other languages. For this very reason, the following fragment won’t compile:

int x;// Error: Use of unassigned local variable 'x'.Console.WriteLine(x);

To ensure variables are properly assigned before they get used, C# has several rules on definite assignment, based on static program flow analysis. I won’t go into detail about those, but I’ll sometimes mention some of the rules in passing.

Assignment is the act of substituting the contents of the storage cell associated with the variable with some value ...

Get C# 4.0 Unleashed 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.