Location>code7788 >text

.Net type value type

Popularity:877 ℃/2025-01-21 10:51:17

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
-128~127
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
-9223372036854775808~9223372036854775807
byte 8-bit signed integer 0~255
ushort
System.UInt16
16-bit signed integer 0~65535
uint System.UInt32
32-bit signed integer
0~4294967295
ulong
System.UInt64
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
Number of digits
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
The float data type is used for smaller floating point values ​​because it requires less precision. The double data type is larger than the float data type and provides twice as much precision (15 bits).
If you encode a non-integer value in your code, the compiler generally assumes that the variable is a double. If you want to specify that the value is a float, you can add the character F or f after it.
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
Literals of type char are enclosed in single quotes, such as 'A'. If you put a character in double quotes, the compiler will treat it as a string and generate an error.
In addition to representing char as a character literal, you can also represent them as a 4-digit hexadecimal Unicode value, an integer value with a cast, or a hexadecimal number.
They can also be expressed as transition sequences as follows.
escape sequence character
\' single quote
\" double quotes
\\ backslash
\0 null
\a
warn
\b Backspace
\f Page change
\n newline
\r Enter
\t
horizontal tab
\v vertical tab