Local Variables when declared must be initialized before they are used, otherwise a compile time error will be raised.
“New-ing” Intrinsic Data Types
All intrinsic data types support what is known as a
default constructor. In a nutshell,
this feature allows you to create a variable using the
new keyword, which automatically sets the variable to its default value:
Although it is more cumbersome to use the
new keyword when creating a basic data type variable,
the following is syntactically well-formed C# code and more advisable.
static void NewingDataTypes()
{
Console.WriteLine("=> Using new to create intrinsic data types:");
bool b = new bool(); // Set to false.
int i = new int(); // Set to 0.
double d = new double(); // Set to 0.
DateTime dt = new DateTime(); // Set to 1/1/0001 12:00:00 AM
Console.WriteLine("{0}, {1}, {2}, {3}", b, i, d, dt);
Console.WriteLine();
}
No comments:
Post a Comment