Advantages/disadvantages of int and Int32

This question already has an answer here:

  • Should I use int or Int32 34 answers

  • They are in fact one and the same -- both declare 32-bit integers, and for the most part their behavior will be identical. The shorthand int is just an alias for the Int32 system type.

    From the language specification:

    4.1.4 Simple Types
    C# provides a set of predefined struct types called the simple types. The simple types are identified through reserved words, but these reserved words are simply aliases for predefined struct types in the System namespace, as described in the table below.

    Here is a list of the simple types and their aliases:

    Reserved word   Aliased type
    sbyte           System.SByte
    byte            System.Byte
    short           System.Int16
    ushort          System.UInt16
    int             System.Int32
    uint            System.UInt32
    long            System.Int64
    ulong           System.UInt64
    char            System.Char
    float           System.Single
    double          System.Double
    bool            System.Boolean
    decimal         System.Decimal
    

    There are only a couple instances I can think of where using one over the other would matter. The first is where it's important to know the limitations of the type (eg cryptography), but that's only for readability. The other is with an enum:

    public enum MyEnum : Int32
    {
        member1 = 0 //no good
    }
    
    public enum MyEnum : int
    {
        member1 = 0 //all is well
    }
    

    There is no any practical advantage or disadvantage .

    The only difference is can be that you esplicitly visualize in case of int32 that you're ddealing with 32 bit value.

    That is.


    int is just an alias to Int32 . So, just use what do you like more.

    链接地址: http://www.djcxy.com/p/21038.html

    上一篇: 在类型转换时使用“int”或“Int32”?

    下一篇: int和Int32的优缺点