English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C# is a strongly typed language. This means that we must declare a variable type, indicating the type of value that will be stored, such as integer, floating-point, decimal, text, and so on.
The following statements and initializations declare and initialize variables of different data types.
string stringVar = "Hello World!!"; int intVar = 100; float floatVar = 10.2f; char charVar = 'A'; bool boolVar = true;
C# mainly divides data types into two types: value types and reference types. Value types include simple types (such as int, float, bool, and char), enum types, struct types, and nullable value types. Reference types include class types, interface types, delegate types, and array types. Value types and reference types will be introduced in detail in the next chapter.
C# includes some predefined value types and reference types. The following table lists the predefined data types:
Type | Description | Range | Suffix |
---|---|---|---|
byte | 8 Bit unsigned integer type | 0 to 255 | |
sbyte | 8 Bit signed integer type | -128 to 127 | |
short | 16 Bit signed integer type | -32,768 to 32,767 | |
ushort | 16 Bit unsigned integer type | 0 to 65,535 | |
int | 32 Bit signed integer type | -2,147,483,648 to 2,147,483,647 | |
uint | 32 Bit unsigned integer type | 0 to 4,294,967,295 | u |
long | 64 Bit signed integer type | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | l |
ulong | 64 Bit unsigned integer type | 0 to 18,446,744,073,709,551,615 | ul |
float | 32 Bit single precision floating point type | -3.402823e38 to 3.402823e38 | f |
double | 64 Bit double precision floating point type | -1.79769313486232e308 to 1.79769313486232e308 | d |
decimal | 128 Bit precise decimal value,28-29 Significant digits | (+ or-)1.0 x 10e-28 to 7.9 x 10e28 | m |
char | 16 Bit Unicode character | Any valid character, such as a,*, \x0058 (hex), or\u0058 (Unicode) | |
bool | 8 Bit logical true/false value | True or False | |
object | Base class of all types | ||
string | Unicode character sequence | ||
DateTime | represents date and time | 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 |
As shown in the table above, each data type (except strings and objects) contains a value range. If the value exceeds the allowed range of the data type, the compiler will produce an error. For example, the range of the int data type is-2,147,483,648to2,147,483,647. Therefore, if the assigned value is not within this range, the compiler will produce an error.
Example: Compile-time error
// Compile-time error: Cannot implicitly convert type 'long' to 'int'. int i = 21474836470;
The values of unsigned integers, long, float, double, and decimal types must be suffixed with u, l, f, d, and m, respectively.
uint ui = 100u; float fl = 10.2f; long l = 45755452222222l; ulong ul = 45755452222222ul; double d = 11452222.555d; decimal mon = 1000.15m;
The predefined data types are aliases for the .NET types (CLR classes). The following table lists the aliases of predefined data types and the corresponding .NET class names.
Alias | .NET types | Type |
---|---|---|
byte | System.Byte | struct |
sbyte | System.SByte | struct |
int | System.Int32 | struct |
uint | System.UInt32 | struct |
short | System.Int16 | struct |
ushort | System.UInt16 | struct |
long | System.Int64 | struct |
ulong | System.UInt64 | struct |
float | System.Single | struct |
double | System.Double | struct |
char | System.Char | struct |
bool | System.Boolean | struct |
object | System.Object | Class |
string | System.String | Class |
decimal | System.Decimal | struct |
DateTime | System.DateTime | struct |
This means that whether you define it as int or Int32variables are the same.
int i = 345; Int32 i = 345;// As above
The values of certain data types may automatically convert to different data types in C#. This is called implicit conversion.
int i = 345; float f = i; Console.WriteLine(f); //Output:345
In the above example, the value of the integer variable i is assigned to the float type variable f because this conversion operation is predefined in C#.
The following is a table of implicit data type conversions.
Implicit conversion | to |
---|---|
sbyte | short, int, long, float, double, decimal |
byte | short, ushort, int, uint, long, ulong, float, double, decimal |
short | int, long, float, double, or decimal |
ushort | int, uint, long, ulong, float, double, or decimal |
int | long, float, double, or decimal. |
uint | long, ulong, float, double, or decimal |
long | float, double, or decimal |
ulong | float, double, or decimal |
char | ushort, int, uint, long, ulong, float, double, or decimal |
float | Double |
Converting from int, uint, long, or ulong to float, and from long or ulong to double may result in a loss of precision. There is no implicit conversion to char type.
However, not all data types are implicitly converted to other data types. For example, int type cannot be implicitly converted to uint. It must be explicitly specified as shown below.
public static void Main() { int i = 100; uint u = (uint) i; Console.Write(i); }
In the above example, the integer i is explicitly converted to uint by specifying uint in the parentheses (uint). This converts the integer to uint.