This feature allows you to create a variable using the new keyword, which automatically sets the variable to its default value:
- bool types are set to false.
- Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
- char types are set to a single empty character.
- DateTime types are set to 1/1/0001 12:00:00 AM.
- Object references (including strings) are set to null.
The following is syntactically well-formed C# code:
//************************************************************************//
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