When does it matter whether you use int versus Int32, or string versus String?

前端 未结 8 656
慢半拍i
慢半拍i 2021-01-19 14:02

In C#, the keywords for built-in types are simply aliases for corresponding types in the System namespace.

Generally, it makes no difference whether yo

8条回答
  •  爱一瞬间的悲伤
    2021-01-19 14:44

    A using alias directive cannot use a keyword as the type name (but can use keywords in type argument lists):

    using Handle = int; // error
    using Handle = Int32; // OK
    using NullableHandle = Nullable; // OK
    

    The underlying type of an enum must be specified using a keyword:

    enum E : int { } // OK
    enum E : Int32 { } // error
    

    The expressions (x)+y, (x)-y, and (x)*y are interpreted differently depending on whether x is a keyword or an identifier:

    (int)+y // cast +y (unary plus) to int
    (Int32)+y // add y to Int32; error if Int32 is not a variable
    (Int32)(+y) // cast +y to Int32
    
    (int)-y // cast -y (unary minus) to int
    (Int32)-y // subtract y from Int32; error if Int32 is not a variable
    (Int32)(-y) // cast -y to Int32
    
    (int)*y // cast *y (pointer indirection) to int
    (Int32)*y // multiply Int32 by y; error if Int32 is not a variable
    (Int32)(*y) // cast *y to Int32
    

提交回复
热议问题