double | System.Double | 64 | Signed 64-bit floating point number; approximately from ±5.0×10-324 to ±1.7×10308 |
float | System.Single | 32 | Signed 32-bit floating point number; approximately from ±1.5×10-45 to ±3.4×1038 |
int | System.Int32 | 32 | Signed 32-bit integer number from -2,147,483,648 to 2,147,483,647 |
uint | System.UInt32 | 32 | Unsigned 32-bit integer number from 0 to 4,294,967,295 |
long | System.Int64 | 64 | Signed 64-bit integer number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
ulong | System.UInt64 | 64 | Unsigned 64-bit integer number from 0 to 18,446,744,073,709,551,615 |
short | System.Int16 | 16 | Signed 16-bit integer number from -32,768 to 32,767 |
ushort | System.UInt16 | 16 | Unsigned 16-bit integer number from 0 to 65,535 |
To declare a variable of a predefined type, you can either use the C# type or the .NET Framework type. For example, to declare an integer variable, you can either use the int or System.Int32
type, as shown here:
int num1 = 5;
//---or---
System.Int32 num2 = 5;
To get the type of a variable, use the GetType()
method:
Console.WriteLine(num1.GetType()); //---System.Int32---
To get the .NET equivalent of a C# type, use the typeof()
method. For example, to learn the .NET type equivalent of C#'s float type, you can use the following statements:
Type t = typeof(float);
Console.WriteLine(t.ToString()); //---System.Single---
To get the size of a type, use the sizeof()
method:
Console.WriteLine('{0} bytes', sizeof(int)); //---4 bytes---
In C#, all noninteger numbers are always treated as a double. And so if you want to assign a noninteger number like 3.99 to a float
variable, you need to append it with the F
(or f
) suffix, like this:
float price = 3.99F;
If you don't do this, the compiler will issue an error message: 'Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type.'
Likewise, to assign a noninteger number to a decimal variable, you need to use the M suffix:
decimal d = 4.56M; //---suffix M to convert to decimal---
float f = 1.23F; //---suffix F to convert to float---
You can also assign integer values using hexadecimal representation. Simply prefix the hexadecimal number with 0x
, like this:
int num1 = 0xA;
Console.WriteLine(num1); //---10---
All value types in C# have a default value when they are declared. For example, the following declaration declares a Boolean and an int
variable:
Boolean married; //---default value is false---
int age; //--- default value is 0---
To learn the default value of a value type, use the default
keyword, like this:
object x; x = default(int);
Console.WriteLine(x); //---0---
x = default(bool);
Console.WriteLine(x); //---false---
However, C# forbids you from using a variable if you do not explicitly initialize it. The following statements, for instance, cause the compiler to complain:
Boolean married;
//---error: Use of unassigned local variable 'married'---
Console.WriteLine(married);
To use the variable, you first need to initialize it with a value:
Boolean married = false;