非唯一的枚举值 [英] Non-unique enum values

查看:84
本文介绍了非唯一的枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图掩盖一个edi文件中的索引位置...我有一种情况,根据情况可以在索引中有2到3个东西。使用枚举来隐藏魔术数字是很酷的,并且惊讶地发现您可以为此同一个值分配多个枚举:

  public enum颜色
{
红色= 1,
蓝色= 1,
绿色= 1
}

,编译器对此很满意。我没想到这样工作。我不需要回到枚举,所以我不用担心要回去,但是这个闻起来很有趣。为什么CLR允许枚举的多个值,我应该使用一个结构体吗? (一个结构似乎比一个枚举更重,这似乎工作)

解决方案

其实你已经定义了一个结构在幕后,枚举只是一个结构体(但是它来源于System.Enum),枚举的值被定义为常量(可以用ILDASM来验证)。



您的枚举定义转换为以下伪C#代码:

  public struct颜色:System.Enum 
{
public const int Red = 1;
public const int Blue = 1;
public const int Green = 1;
}

上面的代码不会在C#中编译,因为编译器不允许定义一个具有明确基类的结构体,但这是它为枚举定义发出的结构。



由于包含多个常量的类型不存在任何问题,相同的值,枚举定义没有问题。



但是,由于枚举没有唯一的值,因此在转换为此枚举时可能会有问题。
例如,以下两行代码将返回枚举值红色,因为第一个值是任意选择的。

 颜色color1 =(颜色)1; 
颜色color2 =(Color)Enum.Parse(typeof(Color),1);

严格来说,枚举值不是红色,它是1,但是当你打印出价值时将会看到红色。



此外,以下布尔值是真的,看起来有点奇怪...

  // true(Red is Green?)
bool b = Color.Red == Color.Green;

在底线,这是完全合法的,但是当你有意义的时候,你可以使用它...



这是一个直接链接到我的.NET教程中讨论枚举的部分: http://motti.me/c1E


I am trying to obscure the index positions on an edi file... I had a situation where 2 or 3 things could be at an index based on the situation. It'd be cool to use an enum to hide the "magic numbers" and was suprised to see that you could assign multiple enums to the same value like this:

public enum Color
{
    Red = 1,
    Blue = 1,
    Green = 1
}

and the compiler is happy with this. I didn't expect this to work. I don't need to cast back to the enum so I'm not worried about trying to go back, but this smells funky. Why does the CLR allow multiple values for enums and should I use a struct for this? (A struct seemed heavier duty than an enum and this seems to work)

解决方案

Actually your'e already defining a struct... Behind the scenes an enum is just a struct (but which derives from System.Enum) and the values of the enum are defined as constants (you can verify this with ILDASM).

Your enum definition translates into the following pseudo C# code:

public struct Color : System.Enum
{
    public const int Red = 1;
    public const int Blue = 1;
    public const int Green = 1;
}

The above code won't compile in C# because the compiler doesn't allow defining a struct with an explicit base class, but that's what it emits for an enum definition.

Since there is no problem with a type that contains an multiple constants that have the same value, there is no problem with the enum definition.

But since the enum does not have unique values you might have an issue when converting into this enum. For example the following two line of codes will return the enum value Red, because the first value is arbitrarily selected.

Color color1 = (Color)1;
Color color2 = (Color)Enum.Parse(typeof(Color), "1");

Strictly speaking the enum value is not Red, it is 1, but when you print out the value you'll see Red.

Also, the following boolean is true which looks a bit weird...

// true (Red is Green??)
bool b = Color.Red == Color.Green;

At the bottom line this is perfectly legal, but it's up to you to use it when it makes sense...

Here is a direct link to the section of my .NET tutorial that discusses enumerations under the hood: http://motti.me/c1E

这篇关于非唯一的枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆