zoukankan      html  css  js  c++  java
  • 编程语言的基元类型

    编译器直接支持的数据类型称为基元类型。基元类型直接映射到Framework类库(FCL)中存在的类型。比如在c#中int直接映射到曹衍涛

    System.Int32类型.

    IL(IL可以指Intermediate Language,同MSIL(Microsoft Intermediate Language),是将.NET代码转化为机器语言的一个中间语言的缩写)

    CLS(common language Specification)公共语言规范

    下为c#基元类型与对应FCL类型

    c#基元类型 FCL类型 CLS相容 说明
    sbyte   System.SByte 有符号8位值
    byte System.Byte 无符号8位值
    short System.Int16 有符号16位值
    ushort System.UInt16 无符号16位值
    int     System.Int32 有符号32位值
    uint System.UInt32 无符号32位值
    long System.Int64 有符号64位值
    ulong System.UInt64 无符号64位值
    char System.Char 16位Unicode字符
    float System.Single IEEE32位浮点值
    double System.Double IEEE64位浮点值
    bool System.Boolean true/false
    decimal System.Declmal 128位高精度浮点值
    string System.String 一个字符数组
    object System.Object 所有类型的基类
    dynamic System.Object 对于CLR,dynamic与objecty完全一致

    编译器能识别常见的编程模式,生成必要的IL,使代码能像预期工作

    c#编译器支持与类型转换、文本常量以及操作符有关的模式

    如果转换时安全的(不会丢失数据),c#允许进行隐式转换,若不安全则要求显示转换。比如说Int32转为Int64可以隐式转换,反之需要显示转换。

    除了转型,基元类型还能写成文本常量,文本常量可以被看作是类型本身的一个实例。

    Console.WriteLine(123.ToString()+123.ToString());//"123123"

    checked与unchecked类型操作

    对基元类型执行的许多操作都可能造成溢出:

    Byte b=100;

    b=(Byte )(b+200);//byte取值范围为-128~127

    程序员可以在特定的区域用checked与unchecked控制溢出检查

    checked操作符

    Byte b=100;

    b=(Byte )checked(b+200);//不会抛出overflowexception异常

    b=checked((Byte )(b+200));//会抛出overflowexception异常

    除了chacked和unchecked操作符,c#还支持checked和unchecked语句,他们造成一个块中所有的表达式都进行或者不进行溢出检查。

    checked{//开始一个checked模块

    Byte b=100;

    b+=200;//该表达式会进行溢出检查

    }//结束一个checked模块

  • 相关阅读:
    Valid Palindrome
    LeetCode: Path Sum 2
    LeetCode: Path Sum
    LeetCode:Convert Sorted Array to Binary Search Tree
    LeetCode:Convert Sorted List to Binary Search Tree
    LeetCode: Balanced Binary Tree
    LeetCode: Maximum Depth of Binary Tree
    django文件上传,只上传一张
    URLError: <urlopen error [Errno 10061] >
    error: <class 'socket.error'>, [Errno 101] Network is unreachable: file: /usr/local/python2.7.12/lib/python2.7/socket.py line: 575
  • 原文地址:https://www.cnblogs.com/lanrenqilanming/p/6258706.html
Copyright © 2011-2022 走看看