C# 类型 | .NET Framework 类型 | |
System.Boolean | ||
System.Byte | ||
System.SByte | ||
System.Char | ||
System.Decimal | ||
System.Double | ||
System.Single | ||
System.Int32 | ||
System.UInt32 | ||
System.Int64 | ||
System.UInt64 | ||
System.Object | ||
System.Int16 | ||
System.UInt16 | ||
System.String |
请记住:在 C# 中不允许使用未初始化的变量。
值类型 | 默认值 |
false | |
0 | |
'\0' | |
0.0D | |
表达式 (E)0 产生的值,其中 E 为 enum 标识符。 | |
0 | |
0 | |
0 | |
将所有的值类型字段设置为默认值并将所有的引用类型字段设置为 null 时产生的值。 | |
0 | |
0 | |
0 |
下表显示了整型的大小和范围,这些类型构成了简单类型的一个子集。
类型 | 范围 | 大小 |
-128 到 127 | 有符号 8 位整数 | |
0 到 255 | 无符号 8 位整数 | |
U+0000 到 U+ffff | 16 位 Unicode 字符 | |
-32,768 到 32,767 | 有符号 16 位整数 | |
0 到 65,535 | 无符号 16 位整数 | |
-2,147,483,648 到 2,147,483,647 | 有符号 32 位整数 | |
0 到 4,294,967,295 | 无符号 32 位整数 | |
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 有符号 64 位整数 | |
0 到 18,446,744,073,709,551,615 | 无符号 64 位整数 |
如果整数表示的值超出了 ulong 的范围,将产生编译错误。
浮点型表(C# 参考)
下表显示了浮点型的精度和大致范围。
类型 | 大致范围 | 精度 |
±1.5e?45 到 ±3.4e38 | 7 位 | |
±5.0e?324 到 ±1.7e308 | 15 到 16 位 |
隐式数值转换表(C# 参考)
下表显示了预定义的隐式数值转换。隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中。
从 | 到 |
short、int、long、float、double 或 decimal | |
short、ushort、int、uint、long、ulong、float、double 或 decimal | |
int、long、float、double 或 decimal | |
int、uint、long、ulong、float、double 或 decimal | |
long、float、double 或 decimal | |
long、ulong、float、double 或 decimal | |
float、double 或 decimal | |
ushort、int、 uint、 long、ulong、 float、double 或 decimal | |
double | |
float、 double 或 decimal |
显式数值转换表(C# 参考)
显式数值转换用于通过显式转换表达式,将任何数字类型转换为任何其他数字类型。对于它不存在隐式转换。下表显示了这些转换。
从 | 到 |
byte、ushort、uint、ulong 或 char | |
Sbyte 或者 char | |
sbyte、 byte、 ushort、 uint、 ulong 或 char | |
sbyte、 byte、 short 或 char | |
sbyte、 byte、 short、 ushort、 uint、 ulong 或 char | |
sbyte、byte、 short、 ushort、 int 或 char | |
sbyte、 byte、 short、 ushort、 int、 uint、 ulong 或 char | |
sbyte、 byte、 short、 ushort、 int、 uint、 long 或 char | |
sbyte、byte 或 short | |
sbyte、 byte、 short、 ushort int、 uint、 long、 ulong、 char 或 decimal | |
sbyte、 byte、 short、 ushort、 int、 uint、 long、 ulong、 char、 float 或 decimal | |
sbyte、 byte、 short、 ushort、 int、 uint、 long、 ulong、 char、 float 或 double |
备注
联系客服