English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# Data Types

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.

Predefined data types in C#

C# includes some predefined value types and reference types. The following table lists the predefined data types:

TypeDescriptionRangeSuffix
byte8 Bit unsigned integer type0 to 255
sbyte8 Bit signed integer type-128 to 127
short16 Bit signed integer type-32,768 to 32,767
ushort 16 Bit unsigned integer type0 to 65,535
int32 Bit signed integer type-2,147,483,648

to

2,147,483,647


uint32 Bit unsigned integer type0 to 4,294,967,295u
long64 Bit signed integer type-9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
l
ulong64 Bit unsigned integer type0 to 18,446,744,073,709,551,615ul
float32 Bit single precision floating point type-3.402823e38 to 3.402823e38f
double64 Bit double precision floating point type-1.79769313486232e308 to 1.79769313486232e308d
decimal128 Bit precise decimal value,28-29 Significant digits(+ or-)1.0 x 10e-28  to 7.9 x 10e28m
char16 Bit Unicode characterAny valid character, such as a,*, \x0058 (hex), or\u0058 (Unicode)
bool8 Bit logical true/false valueTrue or False
objectBase class of all types

string

Unicode character sequence



DateTimerepresents date and time0: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;

Alias and .NET Type

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 typesType
byteSystem.Bytestruct
sbyteSystem.SBytestruct
intSystem.Int32struct
uintSystem.UInt32struct
shortSystem.Int16struct
ushortSystem.UInt16struct
longSystem.Int64struct
ulongSystem.UInt64struct
floatSystem.Singlestruct
doubleSystem.Doublestruct
charSystem.Charstruct
boolSystem.Booleanstruct
objectSystem.ObjectClass
stringSystem.StringClass
decimalSystem.Decimalstruct
DateTimeSystem.DateTimestruct

This means that whether you define it as int or Int32variables are the same.

int i = 345;
Int32 i = 345;// As above

Type conversion

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 conversionto
sbyteshort, int, long, float, double, decimal
byteshort, ushort, int, uint, long, ulong, float, double, decimal
shortint, long, float, double, or decimal
ushortint, uint, long, ulong, float, double, or decimal
intlong, float, double, or decimal.
uintlong, ulong, float, double, or decimal
longfloat, double, or decimal
ulongfloat, double, or decimal
charushort, int, uint, long, ulong, float, double, or decimal
floatDouble

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.