CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
}
Console.WriteLine();
Intrinsic Data Types and the new Operator
All intrinsic data types support what is known as a default constructor (see Chapter 5). This feature
allows you to create a variable using the new keyword, which automatically sets the variable to its default
value.
•
bool variables are set to false.
•
Numeric data is set to 0 (or 0.0 in the case of floating-point data types).
•
char variables are set to a single empty character.
•
BigInteger variables are set to 0.
•
DateTime variables are set to 1/1/0001 12:00:00 AM.
•
Object references (including strings) are set to null.
Note The BigInteger data type seen in the previous list will be explained in just a bit.
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:
static void NewingDataTypes()
{
Console.WriteLine("=> Using new to create variables:");
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();
}
The Data Type Class Hierarchy
It is very interesting to note that even the primitive .NET data types are arranged in a class hierarchy. If
you are new to the world of inheritance, you will discover the full details in Chapter 6. Until then, just
understand that types at the top of a class hierarchy provide some default behaviors that are granted to
the derived types. The relationship between these core system types can be understood as shown in
Figure 3-2.
89