Dixon
Dixon

Reputation: 63

Explicit conversion to enum in c#

How does CLR determine which Color zero should be converted to?

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

    private enum Color
    {
        Red,
        Green = Red
    }
}

Using this Color's definition "Red" will be outputted.

If we use other definitions, results are really very interesting.

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

The output is "Green".

Another definition:

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

The output is "Yellow".

Upvotes: 3

Views: 529

Answers (4)

Pranay Rana
Pranay Rana

Reputation: 176936

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.

Upvotes: 1

Neil
Neil

Reputation: 1633

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502296

It just returns a Color value with an underlying value of 0. That's the same value as Color.Red and Color.Green.

Fundamentally, your enum is broken in that Red and Green are the same value. You can't distinguish between them at all.

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.

Upvotes: 6

NG.
NG.

Reputation: 6063

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.

Upvotes: 0

Related Questions