C#数据类型及范围
来源:互联网 发布:暗黑战神 源码 网盘 编辑:程序博客网 时间:2024/05/12 01:30
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
double
0.0D
enum
表达式 (E)0 产生的值,其中 E 为 enum 标识符。
float
int
0
long
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 位整数
如果整数表示的值超出了 ulong 的范围,将产生编译错误。
浮点型表(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 值。
有关显式转换的更多信息,请参见“
- C#数据类型及范围
- C#数据类型及范围
- c++内置数据类型及范围
- C/C++ 数据类型及范围
- C#数据类型及数据类型转换
- C++基本数据类型大小及表示范围
- Java数据类型及对应取值范围
- Java数据类型及对应取值范围
- MySQL数据类型及范围用法一览表
- MySQL数据类型及范围用法一览表
- MySQL数据类型及范围用法一览表
- byte范围及8种常见数据类型
- Mysql常用数据类型大小及范围
- Java 基本数据类型 范围及默认值
- C语言的数据类型及范围
- 数据类型范围
- 数据类型 范围
- 数据类型范围
- Linux里面编辑器应用
- PHP源代码目录树分析
- .NET绘制条码Code128A,Code128B,Code128C,EAN128
- 什么时候该用commandbuilder
- 代理模式、静态代理、动态代理、aop
- C#数据类型及范围
- 设计模式
- 网页信息采集,来搜网[url]www.laisow.com,全国最便宜的网页数据采服务
- 如何為你的 firefox plugin 加上新的 method
- MYSQL密码找回
- prototype
- 在Oracle中执行动态SQL的几种方法
- 在Win 2003中配置ASP.net环境
- 一大早碰到的一堆小问题