Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

enums

if i declare an enum like

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

Weekdays obj = (Weekdays)1;
Console.WriteLine(obj);//Prints Tue why?

now if i change Weekdays and do the same operation as follows

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

Weekdays obj = (Weekdays)1;
Console.WriteLine(obj);//Prints Thi !!!!!How?

What is really happening here?

like image 764
Shamseer K Avatar asked Dec 12 '22 03:12

Shamseer K


2 Answers

When you write out your value like this, it ends up getting ToString() called on it.

Console.WriteLine(obj);

If you dig into the code far enough down, it's calling Enum.GetName() on your value.

Regarding multiple enum values with the same underlying value, the Enum.GetName page on MSDN says:

If multiple enumeration members have the same underlying value, the 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.

It doesn't state how it determines which name to return if the values on two or more are the same.

The docs for Enum.ToString() include the same warning, in slightly different wording.

Digging a little deeper, the method above makes a call to Array.BinarySearch, passing it an array of numbers representing all values in your enum, and a number representing the value you want to print.

So you have an array with multiple 1's in it, and you're searching for a 1. The docs for that call are similar:

Duplicate elements are allowed. If the Array contains more than one element equal to value, the method returns the index of only one of the occurrences, and not necessarily the first one.

Again, it doesn't state how a selection is made, just that it'll be unreliable.

like image 100
Grant Winney Avatar answered Jan 18 '23 23:01

Grant Winney


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.

like image 37
Syed Farjad Zia Zaidi Avatar answered Jan 19 '23 00:01

Syed Farjad Zia Zaidi