none
Inconsistent behavior when casting integer to an Enum, which has multiple members with same value. RRS feed

  • Question

  • I was experimenting with the below program and I  got the some random output, I also tried to find the pattern how its working, doesn't come up to conclusion   

    class Program { enum Days { Holiday=0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday=Holiday, Sunday=Holiday, } static void Main(string[] args) { var diwali = (Days)0; Console.WriteLine(diwali); }

    }

    Output : Holiday

    If I change the order of Enum value as show in the below then we get some different output 

     enum Days
            {           
                Monday,
                Tuesday,
                Wednesday,
                Thursday,
                Friday,
                Saturday= Holiday,
                Sunday= Holiday,
                Holiday = 0// move it down from top
    
            }

    OP => Saturday

    One as we do more variations output will be change for example

      enum Days
            {
                Sunday = 0,//Moved Sunday from down
                Monday,
                Tuesday,
                Wednesday,
                Thursday,
                Friday,
                Saturday= Holiday,
                Holiday = 0,
    
            }

    output : Sunday

    If we do again some more variations by give different constant value. As shown in the below code  then output will get change

      enum Days
            {
                Sunday = 0,
                Monday,
                Tuesday,          
                Wednesday=1,//assign the constant value,            
                Thursday=2,//assign the constant value,
                Friday,
                Saturday= Holiday,           
                Holiday = 0,
    
            }
     

    Output: Holiday

    So can anybody explain why this behavior? is any algorithm is there for handling the casting?  






    Tuesday, January 16, 2018 12:00 PM

Answers

  • Hi priyanshu parate,

    Thank you for posting here.

    For your question, if multiple enumeration members have the same underlying value, the method you used or GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

    GetName method

        Console.WriteLine(Enum.GetName(typeof(Days), 0));

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 17, 2018 8:48 AM
    Moderator

All replies

  • Hi priyanshu parate,

    Thank you for posting here.

    For your question, if multiple enumeration members have the same underlying value, the method you used or GetName method guarantees that it will return the name of one of those enumeration members. However, it does not guarantee that it will always return the name of the same enumeration member. As a result, when multiple enumeration members have the same value, your application code should never depend on the method returning a particular member's name.

    GetName method

        Console.WriteLine(Enum.GetName(typeof(Days), 0));

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 17, 2018 8:48 AM
    Moderator
  • Hi priyanshu parate,

    Any update? Have you solved your problem?

    If you solved your problem, please mark the useful reply as answer. This will make answer searching in the forum and be beneficial to community members as well.

    If you have something else about this issue, please feel free to contact us.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 21, 2018 1:46 PM
    Moderator
  • Hi @Wendy Zang.

    I am agree with your answer but I am  really looking for the cause means any name picking algorithm is there if it encountered same value constants or its depend on the order of enum name constants 


    Tuesday, January 23, 2018 8:12 AM
  • I'm not sure how you expect any system to return a 'known' value if you define something ambiguously like that.

    The way enums are stored (essentially just as the underlying number) means there is no way any system can unambiguously convert a number back into an enum name.

    In my opinion, using an enum like that is not the way to do things anyway. A 'Days' enum should be a set of days, Monday to Friday. If you want to define some of those days as being a holiday then define some other method somewhere called IsHoliday(Days day).

    Or define your own Days class containing the functionality you need.

    Tuesday, January 23, 2018 8:23 AM