Local variables in C# must be initialized before they are used. For example, you might declare a local variable without initialization as in the following example:
You cannot use it before you initialize it. You can initialize it using the following statement:
myInt = new int(); // Invoke default constructor for int type.
This statement is equivalent to the following statement:
myInt = 0; // Assign an initial value, 0 in this example.
You can, of course, have the declaration and the initialization in the same statement as in the following examples:
–or–
Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. In the preceding example, the default constructor assigned the value 0 to myInt. For more information about values assigned by calling default constructors, see Default Values Table.
With user-defined types, use new to invoke the default constructor. For example, the following statement invokes the default constructor of the Point struct:
Point p = new Point(); // Invoke default constructor for the struct.
After this call, the struct is considered to be definitely assigned; that is, all its members are initialized to their default values.
For more information about the new operator, see new.
For information about formatting the output of numeric types, see Formatting Numeric Results Table.