#36 – Variable Initialization and Definite Assignment
July 23, 2010 4 Comments
Variables must be assigned a value before they can be used. You can declare a variable without initializing it, but attempting to reference the value of the variable before you’ve given it a value will result in a compiler error.
int i; int j = i + 1; // Error: Use of unassigned local variable 'i'
This requirement of definite assignment before use means that you are never able to reference an unassigned variable in C#.
It’s considered good practice to initialize a variable when you declare it. This applies to both built-in types and custom types.
When you initialize a variable, the object that it references is said to be instantiated.
Person p = new Person(); int i = 0;