Using an enum having entries with the same value of underlying type

后端 未结 3 863
遇见更好的自我
遇见更好的自我 2021-01-16 17:28

if i declare an enum like

enum Weekdays
{
    Mon = 1,
    Tue = 1,
    Wen = 1,
    Thi,
    Fri,
    Sat,
    Sun
}

Weekdays obj = (Weekdays)1;
Console.Wr         


        
3条回答
  •  孤街浪徒
    2021-01-16 18:33

    When you assign similar values, the result will be unexpected but I think it will evaluate for two cases:

    When n is even:

    (n/2)
    

    When n is odd:

    (n/2)+1
    

    If I change the enum like this:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1,Fri=1,Sat=1, Sun=1, Mon2=1, Mon3=1}
    // n is odd = 9
    // (n/2)+1 = 5
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result will be Fri, Now lets change the enum again:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1,Fri=1,Sat=1, Sun=1,Mon2=1}
    // n is even = 8
    // (n/2) = 4
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Thi, Again change the enum:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1,Fri=1,Sat=1, Sun=1}
    // n is odd = 7
    // (n/2)+1 = 4
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Thi, Again change the enum:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1,Fri=1,Sat=1}
    // n is even = 6
    // (n/2) = 3
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Wen, Again change the enum:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1,Fri=1}
    // n is odd = 5
    // (n/2)+1 = 3
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Wen, Changing the enum again:

    enum Weekdays {Mon=1,Tue=1,Wen=1,Thi=1}
    // n is even = 4
    // (n/2) = 2
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Tue, Changing the enum again:

    enum Weekdays {Mon=1,Tue=1,Wen=1}
    // n is odd = 3
    // (n/2)+1 = 2
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);
    

    The result is now Tue.

    Even though this is explaining the behavior perfectly but this may not always happen or may not happen since I have not checked this for more cases but as MSDN says you should not assume about such output when the enum have same values for different names...

    That said, I think you can easily understand now what is happening in your code.

    Ref.: Link

    Edit:

    @GrantWinney's answer led me to this, He has written that Array.BinarySearch is passed the array of values and the value to search for so I realized from the name Array.BinarySearch that it is definitely using a BinarySearch and that explains everything...

    Binary Search will divide the array like this:

    Mid = {Low(which is the starting index) + High (which is the last index of array)}/2
    

    and then Check for

    if (Mid == value) return index;
    else 
     if the value is smaller or equal move left other wise move right of the array
    

    So this explains it how the enum values are printed if their are multiple names for the value you are trying to print.

    Your Original Question

    enum Weekdays
    {
        Mon = 1,
        Tue = 1,
        Wen = 1,
        Thi,
        Fri,
        Sat,
        Sun
    }
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);//Prints Tue why?
    

    It prints Tue because a call to Array.BinarySearch will be made passing the array

    {1, 1, 1, 2, 3, 4, 5} 
    

    and a value to search which is 1...

    So the BinarySearch will do this:

    Mid = {Low(0) + High(6)} / 2
    if (Mid == value) return index
    else move left 
    

    After moving left again the Mid will be calculated:

    High = Mid - 1; // now only the left sub-array will be searched
    Mid = {Low(0) + High(2)} / 2
    if (Mid == value) return index // here the condition will be true and you will be returned with `Tue`
    

    The 2nd example in your Question:

    enum Weekdays
    {
        Mon = 1,
        Tue = 1,
        Wen = 1,
        Thi = 1,
        Fri,
        Sat,
        Sun
    }
    
    Weekdays obj = (Weekdays)1;
    Console.WriteLine(obj);//Prints Thi !!!!!How?
    

    As I have written above a call to Array.BinarySearch will be made and array:

    {1, 1, 1, 1, 2, 3, 4} 
    

    will be passed with value = 1 to search...

    Apply the BinarySearch algorithm on the array and it will evaluate to Thi.

提交回复
热议问题