为什么(以及如何)Enum 的顺序影响 ToString 值?
我对枚举值的“顺序”有疑问。这有点难以解释,这就是为什么我写了一些代码:
class Program
{
public enum EnumA
{
One = 1,
Two = One,
Three = Two,
Four = 4
}
public enum EnumB
{
One = 1,
Two = One,
Four = 4,
Three = Two
}
public enum EnumC
{
Two = One,
Three = Two,
Four = 4,
One = 1
}
static void Main(string[] args)
{
Console.WriteLine("Enum A:");
Console.WriteLine(EnumA.One);
Console.WriteLine(EnumA.Two);
Console.WriteLine(EnumA.Three);
Console.WriteLine(EnumA.Four);
Console.WriteLine();
Console.WriteLine("Enum B:");
Console.WriteLine(EnumB.One);
Console.WriteLine(EnumB.Two);
Console.WriteLine(EnumB.Three);
Console.WriteLine(EnumB.Four);
Console.WriteLine();
Console.WriteLine("Enum C:");
Console.WriteLine(EnumC.One);
Console.WriteLine(EnumC.Two);
Console.WriteLine(EnumC.Three);
Console.WriteLine(EnumC.Four);
Console.WriteLine();
Console.ReadLine();
}
}
输出是:
Enum A: 二 二 二 四个
枚举 B: 三 三 三 四个
枚举 C: 一 一 一 四
我的问题是:为什么!?我找不到输出的逻辑。大多数时候都是有逻辑可寻的,所以我希望你们能对这个问题有所启发。
我使用VS2010 / .Net 4.0编译并运行代码。
I have problems with the "order" of the values of an enum. It's a little difficult to explain, that's why I wrote up some code:
class Program
{
public enum EnumA
{
One = 1,
Two = One,
Three = Two,
Four = 4
}
public enum EnumB
{
One = 1,
Two = One,
Four = 4,
Three = Two
}
public enum EnumC
{
Two = One,
Three = Two,
Four = 4,
One = 1
}
static void Main(string[] args)
{
Console.WriteLine("Enum A:");
Console.WriteLine(EnumA.One);
Console.WriteLine(EnumA.Two);
Console.WriteLine(EnumA.Three);
Console.WriteLine(EnumA.Four);
Console.WriteLine();
Console.WriteLine("Enum B:");
Console.WriteLine(EnumB.One);
Console.WriteLine(EnumB.Two);
Console.WriteLine(EnumB.Three);
Console.WriteLine(EnumB.Four);
Console.WriteLine();
Console.WriteLine("Enum C:");
Console.WriteLine(EnumC.One);
Console.WriteLine(EnumC.Two);
Console.WriteLine(EnumC.Three);
Console.WriteLine(EnumC.Four);
Console.WriteLine();
Console.ReadLine();
}
}
The output is:
Enum A:
Two
Two
Two
Four
Enum B:
Three
Three
Three
Four
Enum C:
One
One
One
Four
My question is: WHY!? I can't find the logic to the output. Most of the time there is some logic to be found, so I hope you guys can shine some light on this issue.
I used VS2010 / .Net 4.0 to compile and run the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该行为被指定为“未定义”(我以为我刚才发现了一个模式,但显然没有。) aspx">文档明确指出了这一点:
要么使您的枚举值不同,要么显式创建从值到所需名称的映射。
The behaviour is specified to be "undefined" (I thought I'd spotted a pattern just now, but apparently not.) The documentation explicitly calls this out:
Either make your enum values distinct, or explicitly create a map from value to desired name.
如果反编译 IL,首先要观察到的是对
WriteLine
的调用看起来非常相似:也就是说,这些枚举值的加载是加载值“1”三次,并且然后调用
WriteLine
。因此,我们不应该对前 3 个调用都产生相同值感到惊讶。我已经尝试了一些实验,但无法指出您可以依赖的任何特定(未记录)行为来预测将打印哪个值。
The first thing to observe, if you decompile the IL, is that the calls to
WriteLine
all look remarkably similar:That is, the loading of these enum values is loading the value "1" three times, and then calling
WriteLine
. So we should not be surprised that the 1st 3 calls all result in the same value.I've tried a few experiments, but can't point to any particular (undocumented) behaviour you can rely on to predict which value will be printed.