Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit conversion to enum in c#

Tags:

c#

enums

clr

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".

like image 417
Dixon Avatar asked Jun 22 '26 03:06

Dixon


2 Answers

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.

like image 125
Jon Skeet Avatar answered Jun 23 '26 18:06

Jon Skeet


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.

like image 39
Neil Avatar answered Jun 23 '26 18:06

Neil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!