C# is a strongly typed language and as such all variables and objects must have a declared data type. The data type can be one of the following:
□ Value
□ Reference
□ User-defined
□ Anonymous
You'll find more information about user-defined types in Chapter 4 and about anonymous types in Chapter 14.
Value Types
A value type variable contains the data that it is assigned. For example, when you declare an int (integer) variable and assign a value to it, the variable directly contains that value. And when you assign a value type variable to another, you make a copy of it. The following example makes this clear:
class Program {
static void Main(string[] args) {
int num1, num2;
num1 = 5;
num2 = num1;
Console.WriteLine('num1 is {0}. num2 is {1}', num1, num2);
num2 = 3;
Console.WriteLine('num1 is {0}. num2 is {1}', num1, num2);
Console.ReadLine();
return;
}
}
The output of this program is:
num1 is 5. num2 is 5
num1 is 5. num2 is 3
As you can observe, num2
is initially assigned a value of num1
(which is 5). When num2
is later modified to become 3, the value of num1
remains unchanged (it is still 5). This proves that the num1
and num2
each contains a copy of its own value.
Following is another example of value type. Point is a structure that represents an ordered pair of integer x and y coordinates that defines a point in a two-dimensional plane (structure is another example of value types). The Point
class is found in the System.Drawing
namespace and hence to test the following statements you need to import the System.Drawing
namespace.
Chapter 4 discusses structures in more detail.
Point pointA, pointB;
pointA = new Point(3, 4);
pointB = pointA;
Console.WriteLine('point A is {0}. pointB is {1}',
pointA.ToString(), pointB.ToString());
pointB.X = 5;
pointB.Y = 6;
Console.WriteLine('point A is {0}. pointB is {1}',
pointA.ToString(), pointB.ToString());
These statements yield the following output:
point A is {X=3,Y=4}. pointB is {X=3,Y=4}
point A is {X=3,Y=4}. pointB is {X=5,Y=6}
As in the earlier example, changing the value of the pointB does not change the value of pointA.
The .NET Framework ships with a set of predefined C# and .NET value types. These are described in the following table.
C# Type | .NET Framework Type | Bits | Range |
---|---|---|---|
bool | System.Boolean | True or false | |
byte | System.Byte | 8 | Unsigned 8-bit integer values from 0 to 255 |
sbyte | System.SByte | 8 | Signed 8-bit integer values from -128 to 127 |
char | System.Char | 16 | 16-bit Unicode character from U+0000 to U+ffff |
decimal | System.Decimal | 128 | Signed 128-bit number from ±1.0×10-28 to ±7.9×1028 |