【问题标题】:MSIL of System.Int32 and int will be same? [duplicate]System.Int32 和 int 的 MSIL 会相同吗? [复制]
【发布时间】:2011-05-31 11:49:42
【问题描述】:

可能重复:
C#, int or Int32? Should I care?

请任何人告诉我 System.Int32 的 MSIL 和 int 将相同或不同,如果不同,那么我们应该使用哪一个。

编辑:

这不会编译

public  enum MyEnum : Int32
{
    AEnum = 0
}

但这会

public enum MyEnum : int
{
    AEnum = 0
}

【问题讨论】:

标签: c# .net asp.net


【解决方案1】:

intSystem.Int32的别名,完全一样。

enums 仅将整数类型作为类型,否则这是可能的:

using Int32 = System.String;

public enum Something : Int32
{
}

这是根据 C# 规范规定的

enum-declaration:
[attributes] [enum-modifiers ] enum identifier [enum-base] enum-body [;]

where
enum-base:
":" integral-type

整数类型指定为:

integral-type:
sbyte
byte
short
ushort
int
uint
long
ulong
char

【讨论】:

  • 我已经编辑了我的问题,请立即查看
  • @Pankaj:您的编辑显示了一个小的极端情况,其中intInt32 不能互换,在这种情况下编译器会告诉您这一点。您可以放心地假设,只要编译器不产生任何错误,则生成的 IL 将与 intInt32 完全相同。
【解决方案2】:

http://msdn.microsoft.com/en-us/library/5kzh1b5w(v=VS.100).aspx

类型:int
.NET 框架类型:System.Int32

【讨论】:

  • 我已经编辑了我的问题,请立即查看
【解决方案3】:

两者相同,但 intlanguage specification is the definitive source for C# syntax and usage

Int32 是一个不可变的值类型,它表示有符号整数 范围从负的值 2,147,483,648(表示为 Int32.MinValue 常量)通过 正 2,147,483,647(即 由 Int32.MaxValue 表示 持续的。 来自MSDN

【讨论】:

    【解决方案4】:

    intSystem.Int32 的别名,所以没有区别

    【讨论】:

      【解决方案5】:

      这些与每个人已经告诉过的完全相同,intInt32 的别名。

      您可以自己进行实验,将鼠标放在 Visual Studio 中的 int 上,然后单击 “Go to Decleration”。 与int相同string也是String的别名

      【讨论】:

        【解决方案6】:

        确认。 System.Int32 不起作用。 Mono 2.8 编译器 sais:

        t.cs(3,29): error CS1008: Type byte, sbyte, short, ushort, int, 
        uint, long or ulong expected
        

        所以它似乎是一个硬编码的语言定义规则。


        在这里试图成为魔鬼的拥护者:

        也许您遇到了名称冲突,并且您在其他地方定义了 Int32(我没有尝试过这是否合法,但这可能是个问题)。尝试限定它:

         public  enum MyEnum : System.Int32
         {
            AEnum = 0
         }
        

        【讨论】:

        • System.Int32 也无法正常工作,您可以检查一次吗?
        猜你喜欢
        • 1970-01-01
        • 2012-12-13
        • 1970-01-01
        • 1970-01-01
        • 2011-07-17
        • 1970-01-01
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多