C#数据类型及范围
2008-12-10 10:33
225 查看
C# 类型 | | .NET Framework 类型 |
bool | | System.Boolean |
byte | | System.Byte |
sbyte | | System.SByte |
char | | System.Char |
decimal | | System.Decimal |
double | | System.Double |
float | | System.Single |
int | | System.Int32 |
uint | | System.UInt32 |
long | | System.Int64 |
ulong | | System.UInt64 |
object | | System.Object |
short | | System.Int16 |
ushort | | System.UInt16 |
string | | System.String |
请记住:在 C# 中不允许使用未初始化的变量。
值类型 | 默认值 |
bool | false |
byte | 0 |
char | '/0' |
decimal | 0.0M |
double | 0.0D |
enum | 表达式 (E)0 产生的值,其中 E 为 enum 标识符。 |
float | 0.0F |
int | 0 |
long | 0L |
sbyte | 0 |
short | 0 |
struct | 将所有的值类型字段设置为默认值并将所有的引用类型字段设置为 null 时产生的值。 |
uint | 0 |
ulong | 0 |
ushort | 0 |
下表显示了整型的大小和范围,这些类型构成了简单类型的一个子集。
类型 | 范围 | 大小 |
sbyte | -128 到 127 | 有符号 8 位整数 |
byte | 0 到 255 | 无符号 8 位整数 |
char | U+0000 到 U+ffff | 16 位 Unicode 字符 |
short | -32,768 到 32,767 | 有符号 16 位整数 |
ushort | 0 到 65,535 | 无符号 16 位整数 |
int | -2,147,483,648 到 2,147,483,647 | 有符号 32 位整数 |
uint | 0 到 4,294,967,295 | 无符号 32 位整数 |
long | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 | 有符号 64 位整数 |
ulong | 0 到 18,446,744,073,709,551,615 | 无符号 64 位整数 |
浮点型表(C# 参考)
下表显示了浮点型的精度和大致范围。
类型 | 大致范围 | 精度 |
float | ±1.5e−45 到 ±3.4e38 | 7 位 |
double | ±5.0e−324 到 ±1.7e308 | 15 到 16 位 |
隐式数值转换表(C# 参考)
下表显示了预定义的隐式数值转换。隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中。
从 | 到 |
sbyte | short、int、long、float、double 或 decimal |
byte | short、ushort、int、uint、long、ulong、float、double 或 decimal |
short | int、long、float、double 或 decimal |
ushort | int、uint、long、ulong、float、double 或 decimal |
int | long、float、double 或 decimal |
uint | long、ulong、float、double 或 decimal |
long | float、double 或 decimal |
char | ushort、int、 uint、 long、ulong、 float、double 或 decimal |
float | double |
ulong | float、 double 或 decimal |
从 int、uint 或 long 到 float
的转换以及从 long 到 double 的转换的精度可能会降低,但数值大小不受影响。
不存在到 char 类型的隐式转换。
不存在浮点型与 decimal 类型之间的隐式转换。
int 类型的常数表达式可转换为 sbyte、byte、short、ushort、uint 或 ulong,前提是常数表达式的值处于目标类型的范围之内。
显式数值转换表(C# 参考)
显式数值转换用于通过显式转换表达式,将任何数字类型转换为任何其他数字类型。对于它不存在隐式转换。下表显示了这些转换。
从 | 到 |
sbyte | byte、ushort、uint、ulong 或 char |
byte | Sbyte 或者 char |
short | sbyte、 byte、 ushort、 uint、 ulong 或 char |
ushort | sbyte、 byte、 short 或 char |
int | sbyte、 byte、 short、 ushort、 uint、 ulong 或 char |
uint | sbyte、byte、 short、 ushort、 int 或 char |
long | sbyte、 byte、 short、 ushort、 int、 uint、 ulong 或 char |
ulong | sbyte、 byte、 short、 ushort、 int、 uint、 long 或 char |
char | sbyte、byte 或 short |
float | sbyte、 byte、 short、 ushort int、 uint、 long、 ulong、 char 或 decimal |
double | sbyte、 byte、 short、 ushort、 int、 uint、 long、 ulong、 char、 float 或 decimal |
decimal | sbyte、 byte、 short、 ushort、 int、 uint、 long、 ulong、 char、 float 或 double |
显式数值转换可能导致精度损失或引发异常。
将 decimal 值转换为整型时,该值将舍入为与零最接近的整数值。如果结果整数值超出目标类型的范围,则会引发 OverflowException。
将 double 或 float 值转换为整型时,值会被截断。如果该结果整数值超出了目标值的范围,其结果将取决于溢出检查上下文。在 checked 上下文中,将引发 OverflowException;而在 unchecked 上下文中,结果将是一个未指定的目标类型的值。
将 double 转换为 float 时,double 值将舍入为最接近的 float 值。如果 double 值因过小或过大而使目标类型无法容纳它,则结果将为零或无穷大。
将 float 或 double 转换为 decimal 时,源值将转换为 decimal 表示形式,并舍入为第 28 个小数位之后最接近的数(如果需要)。根据源值的不同,可能产生以下结果:
如果源值因过小而无法表示为 decimal,那么结果将为零。
如果源值为 NaN(非数字值)、无穷大或因过大而无法表示为 decimal,则会引发 OverflowException。
将 decimal 转换为 float 或 double 时,decimal 值将舍入为最接近的 double 或 float 值。
有关显式转换的更多信息,请参见“6.2 C# 语言规范中的显式”。有关如何访问此规范的更多信息,请参见 C# 语言规范。
相关文章推荐
- 【C#】 数据类型的取值范围和精度
- C#中各种数据类型可以表示的数据的范围【转】
- C#中各种数据类型可以表示的数据的范围
- c#值类型数据的占用字节数和范围
- C中数据类型数值范围解释
- C#中结构数据类型的使用
- C#基础:关键字和数据类型
- java基本数据类型取值范围
- C#的变量、数据类型转换、转义符
- C#学习基本概念之数据类型
- C# 数据类型和字节长度
- SQL数据类型和C#数据类型间的转换
- C# winform关于datagridview中的列的数据类型转换问题
- C#的数据类型和表达式
- 第五 讲 : 预定义数据类型 -【天轰穿.Net4趣味编程系列视频教程-vs2010轻松学习C#】
- C中数据类型数值范围解析
- nullable数据类型和c#2.0运算符“??”,数据库可能为空时候的一种处理办法
- c#总结——数据类型与运算符
- C#中数组数据类型字符串转double型问题
- c#数据类型转换,与其BYTE,float,double,char类型间的转换方法