Predefined type value types Data type C# keywords (such as int, short, and string) are mapped from the compiler to .NET data types. For example, when declaring an int type data in C#, what is actually declared is an instance of .Net System.Int32. This may seem esoteric, but its implications are far-reaching; it means that syntactically, all primitive types can be thought of as classes that support some methods. For example, to convert int i to string type, you can write the following code;
string s = ();
It should be emphasized that behind this syntax, types are actually still stored as primitive types. Primitive types are conceptually represented as C# structs, so there's definitely no performance penalty.
1. Plastic surgery
C# supports 8 predefined integer types
name | .Net type | illustrate | Range (minimum~maximum) |
sByte | 8-bit signed integer |
|
|
short | System.Int16 | 16-bit signed integer | -32768~32767 |
int | System.Int32 | 32-bit signed integer | -2147483648~2147483647 |
long | System.Int64 | 64-bit signed integer |
|
byte | 8-bit signed integer | 0~255 | |
ushort |
|
16-bit signed integer | 0~65535 |
uint | System.UInt32 |
|
0~4294967295 |
ulong |
|
64-bit signed integer | 0~18446744073709551615 |
2. Floating point type
C# provides support for many integer data types and also supports floating point types.
name | .Net type | illustrate |
|
Range (minimum~maximum) |
float |
|
32-bit single precision floating point number | 7 | -3.40282347E+38F~3.40282347E+38F |
double | 64-bit double precision floating point number | 15/16 | -1.7976931348623157E+308~1.7976931348623157E+308 |
float f = 12.3F;
type
The decimal type represents a floating point number with higher precision.
name | .Net type | illustrate | Number of digits | Range (minimum~maximum) |
decimal | 128-bit high-precision decimal number representation | 28 | -79228162514264337593543950335M~79228162514264337593543950335M |
An important advantage of .Net and C# data types is that a specialized type is provided for financial calculations, which is how the decimal type provides 28 bits. It is up to the user. In other words, you can express smaller dollar values with greater precision (with cents), or you can express larger dollar values with more rounding in the decimal part. However, it should be noted that the decimal type is not a basic type, so there will be a performance loss when using this type in calculations.
To specify a number as decimal type instead of double, float or integer type, you can add the character M or m after the number, such as:
decimal d = 12.3M;
4.Boolean type
C#’s bool type is used to contain Boolean values true or false
name | .Net type | illustrate | Number of digits | Range (minimum~maximum) |
bool | Indicates true or false | NA | true or false |
Bool values and integer values cannot be converted to each other. If the variable (or function return type) is declared as bool, you can only use true or false. If you try to use 0 to represent false and a non-zero value to represent true, you will get an error.
bool a = false;
bool b = 1>0;
5.Character type
To save the value of a single character, C# supports the char data type
name | .Net type | value |
char | Represents a 16-bit (Unicode) character |
escape sequence | character |
\' | single quote |
\" | double quotes |
\\ | backslash |
\0 | null |
\a |
|
\b | Backspace |
\f | Page change |
\n | newline |
\r | Enter |
\t |
|
\v | vertical tab |