3

CLR 如何确定应转换为哪种颜色零?

internal static class Test
{
    private static void Main()
    {
        Console.WriteLine((Color)0);
    }

    private enum Color
    {
        Red,
        Green = Red
    }
}

使用此颜色的定义“红色”将被输出。

如果我们使用其他定义,结果真的很有趣。

private enum Color
{
    Red,
    Green = Red,
    Blue = Red,
    Yellow = Red
}

输出为“绿色”。

另一个定义:

private enum Color
{
    Red,
    Green = Red,
    Blue,
    Yellow  = Red
}

输出为“黄色”。

4

4 回答 4

6

它只返回一个Color基础值为 0 的值。这与Color.Redand的值相同Color.Green

从根本上说,您的枚举在其中被破坏Red并且Green具有相同的值。你根本无法区分它们。

Color red = Color.Red;
Color green = Color.Green;
Console.WriteLine(red == green); // True
Console.WriteLine(red.ToString() == green.ToString()); // True

I don't know if there are any guarantees around whether ToString returns "Red" or "Green" - but if you get to the situation where that's relevant, you should have described your enum differently.

EDIT: From the documentation:

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value.

...

The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.

于 2012-07-21T08:14:57.357 回答
1

Each enum variable in .net map to integer value so from that only it get to know which type to return

MSDN- enum (C# Reference)

The default underlying type of the enumeration elements is int. By default, the
first enumerator has the value 0, and the value of each successive enumerator is 
 increased by 1.
于 2012-07-21T08:16:51.210 回答
1

The answer for something like this is - undefined. You may find that the answer changes from version to version, or even machine to machine (less likely). While the question is interesting - you should NEVER rely on any particular answer being produced in a situation like this. There are plenty of other ways to go about that you are trying to do.

Like:

 private enum MainColor
    {
        Red,
        Green, 
        Blue, 
    }

   private enum Color
   {
        Red = MainColor.Red,
        Green = MainColor.Green, 
        Blue = MainColor.Blue, 
        Brown = MainColor.Red, 
   }

You can then cast back to MainColor and get a definitive answer.

It seems to me that it is coming up with an the first match it finds based on some kind of hashed list. The details should never matter to your code.

于 2012-07-21T22:15:10.477 回答
0

Instead of your enum try this :

    private enum Color
    {  
         Red,
         Green,BLUE,black,brown = Red,gray  
    }

Observations :

1) (Color)0 : Red  
2) (Color)1 : Green   
3) (Color)2 : BLUE  
4) (Color)3 : black  

so CLR will take it in the order which they come in enum.

于 2012-07-21T08:32:56.860 回答