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

前端 未结 8 649
慢半拍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:25

    It doesn't matter. int is basically a shorthand for System.Int32 they are almost perfectly interchangable. Consider it a #define if you like (but C# doesn't allow #defines like that).

    0 讨论(0)
  • 2021-01-19 14:30

    One issue is that with strange usings or member declarations String/Int32 can map to a different type(i.e. not the one from mscorlib) or even another kind of member, which would lead to problems.

    As Jon mentioned, another issue is that you can't declare enums using an identifier(You get the error "Type byte, sbyte, short, ushort, int, uint, long, or ulong expected").

    0 讨论(0)
  • 2021-01-19 14:34

    It doesn't require you to use one or the other. However, if you want a larger or smaller int you might want to explicitly use Int16 or Int64 instead of int which is an alias for Int32.

    0 讨论(0)
  • 2021-01-19 14:36

    The only situation I can think of in which you are required to use the aliases in a form where you might otherwise be able to use a class name is in the definition of enum types:

    public enum MyEnum:Byte //will not compile
    
    public enum MyEnum:byte //correct
    

    Notice also that the definition of an enum requires use of the alias keyword, while when defining code members that can accept any enumerated type, you use the class name Enum.

    Lastly, you can never specify System.ValueType as a base class or generic type parameter; you instead use the struct keyword, which is essentially an alias for an object deriving from ValueType.

    0 讨论(0)
  • 2021-01-19 14:42

    The identifiers int, string, bool, etc. are C# language aliases for the real types Int32, String, and Boolean, respectively. It doesn't matter which you use, but when you're writing an API some people prefer to use the actual class types to the aliases.

    Here's an MSDN article that lists the equivalents.

    0 讨论(0)
  • 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<int>; // 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
    
    0 讨论(0)
提交回复
热议问题