【发布时间】:2011-05-31 11:49:42
【问题描述】:
请任何人告诉我 System.Int32 的 MSIL 和 int 将相同或不同,如果不同,那么我们应该使用哪一个。
编辑:
这不会编译
public enum MyEnum : Int32
{
AEnum = 0
}
但这会
public enum MyEnum : int
{
AEnum = 0
}
【问题讨论】:
请任何人告诉我 System.Int32 的 MSIL 和 int 将相同或不同,如果不同,那么我们应该使用哪一个。
编辑:
这不会编译
public enum MyEnum : Int32
{
AEnum = 0
}
但这会
public enum MyEnum : int
{
AEnum = 0
}
【问题讨论】:
int是System.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
【讨论】:
int 和Int32 不能互换,在这种情况下编译器会告诉您这一点。您可以放心地假设,只要编译器不产生任何错误,则生成的 IL 将与 int 或 Int32 完全相同。
http://msdn.microsoft.com/en-us/library/5kzh1b5w(v=VS.100).aspx
类型:int
.NET 框架类型:System.Int32
【讨论】:
两者相同,但 int 是 language specification is the definitive source for C# syntax and usage
Int32 是一个不可变的值类型,它表示有符号整数 范围从负的值 2,147,483,648(表示为 Int32.MinValue 常量)通过 正 2,147,483,647(即 由 Int32.MaxValue 表示 持续的。 来自MSDN
【讨论】:
int 是System.Int32 的别名,所以没有区别
【讨论】:
这些与每个人已经告诉过的完全相同,int 是Int32 的别名。
您可以自己进行实验,将鼠标放在 Visual Studio 中的 int 上,然后单击 “Go to Decleration”。
与int相同string也是String的别名
【讨论】:
确认。 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
}
【讨论】: