zoukankan      html  css  js  c++  java
  • C#数据类型及范围

    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 位整数

    如果整数表示的值超出了 ulong 的范围,将产生编译错误。

    浮点型表(C# 参考)

    下表显示了浮点型的精度和大致范围。

    类型

    大致范围

    精度

    float

    ±1.5e−45 ±3.4e38

    7

    double

    ±5.0e−324 ±1.7e308

    15 16

     

    隐式数值转换表(C# 参考)

    下表显示了预定义的隐式数值转换。隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中。

    sbyte

    shortintlongfloatdouble decimal

    byte

    shortushortintuintlongulongfloatdouble decimal

    short

    intlongfloatdouble decimal

    ushort

    intuintlongulongfloatdouble decimal

    int

    longfloatdouble decimal

    uint

    longulongfloatdouble decimal

    long

    floatdouble decimal

    char

    ushortint uint longulong floatdouble decimal

    float

    double

    ulong

    float double decimal

    <!--[if !vml]--><!--[endif]--> 备注

    • intuint long float 的转换以及从 long double 的转换的精度可能会降低,但数值大小不受影响。
    • 不存在到 char 类型的隐式转换。
    • 不存在浮点型与 decimal 类型之间的隐式转换。
    • int 类型的常数表达式可转换为 sbytebyteshortushortuint ulong,前提是常数表达式的值处于目标类型的范围之内。

    显式数值转换表(C# 参考)

    显式数值转换用于通过显式转换表达式,将任何数字类型转换为任何其他数字类型。对于它不存在隐式转换。下表显示了这些转换。

    sbyte

    byteushortuintulong 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

    sbytebyte short ushort int char

    long

    sbyte byte short ushort int uint ulong char

    ulong

    sbyte byte short ushort int uint long char

    char

    sbytebyte 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 值。
  • 相关阅读:
    javascript函数apply和call
    【剑指offer】面试题42. 连续子数组的最大和
    【SQL】排名
    【剑指offer】面试题21. 调整数组顺序使奇数位于偶数前面
    【剑指offer】 面试题29. 顺时针打印矩阵
    【剑指offer】面试题58
    【剑指offer】面试题34. 二叉树中和为某一值的路径
    【SQL】取第n个
    【剑指offer】面试题55
    【剑指offer】面试题55
  • 原文地址:https://www.cnblogs.com/emanlee/p/1910804.html
Copyright © 2011-2022 走看看