6

I wrote the code :

enum FlipRotate2dEnum : byte {
    NO = 0,    None               = 0,
    R2 = 1,    RotateTwice        = 1,
    FX = 2,    FlipX              = 2,
    FY = 3,    FlipY              = 3,
    D1 = 4,    ReflectDiagonal1   = 4,
    D2 = 5,    ReflectDiagonal2   = 5,
    RC = 6,    RotateClockwise    = 6,
    RN = 7,    RotateNonClockwise = 7
}
class EnumTest {
    public static void Main() {
        for(byte i = 0; i < 8; ++i) {
            FlipRotate2dEnum v = (FlipRotate2dEnum)i;
            System.Console.WriteLine("{0} {1}", i, v);
        }
    }
}

and I expect to see in ouput :

only short names

0 NO
1 R2
2 FX
3 FY
4 D1
5 D2
6 RC
7 RN

or only long names

0 None
1 RotateTwice
2 FlipX
3 FlipY
4 ReflectDiagonal1
5 ReflectDiagonal2
6 RotateClockwise
7 RotateNonClockwise

or names that occur first, after sorting in alphabetical order, which in this case coincides with "only short names".

But I did not expect to see what the program showed up :

0 None
1 RotateTwice
2 FlipX
3 FlipY
4 ReflectDiagonal1
5 ReflectDiagonal2
6 RotateClockwise
7 RN

Short name at the end of the output. ¿ Why ?


I tried to rearrange columns in enum :

public enum FlipRotate2dEnum : byte {
    None               = 0, NO = 0,
    RotateTwice        = 1, R2 = 1,
    FlipX              = 2, FX = 2,
    FlipY              = 3, FY = 3,
    ReflectDiagonal1   = 4, D1 = 4,
    ReflectDiagonal2   = 5, D2 = 5,
    RotateClockwise    = 6, RC = 6,
    RotateNonClockwise = 7, RN = 7
}
class EnumTest {
    public static void Main() {
        for(byte i = 0; i < 8; ++i) {
            FlipRotate2dEnum v = (FlipRotate2dEnum)i;
            System.Console.WriteLine("{0} {1}", i, v);
        }
    }
}

and again I got a surprise in the output :

0 NO
1 R2
2 FX
3 FY
4 D1
5 D2
6 RC
7 RotateNonClockwise

¿ Why ? Please, explain to me what's going on here.

3
  • 5
    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. May 11, 2018 at 2:39
  • 3
    It would probably be better to make use of the [Description] attribute and create a method to retrieve the value contained within for a given enum value. May 11, 2018 at 2:41
  • 1
    As john suggested [Description] will be most suitable solution here, still anyhow you are not able to use it. you can use const offset to distinguishes between two values. like None = 0, NO = ConstCarrier.Offset + 0,
    – Amit
    May 11, 2018 at 2:48

1 Answer 1

1

Enum variable is basically just a number, it doesn't have a label (such as R2) attached to it. For example, FlipRotate2dEnum.RN and FlipRotate2dEnum.RotateNonClockwise are the same thing, because they have the same value 7. Actually if you do:

Console.WriteLine(FlipRotate2dEnum.RotateNonClockwise);

You will see RN as output. As stated in documentation of Enum.ToString()

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

But if you are curious how exactly result you see is obtained internally, please read below.

First, array of underlying values (in your case - bytes) is obtained roughly like this (but not exactly like this):

byte[] values = Enum.GetValues(typeof(FlipRotate2dEnum)).Cast<byte>().ToArray();

It does not contain distinct values, just all values in enum, sorted. So in this case 16 values:

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7

Then array of names is obtained (again, not exactly like this, but in a similar way). This array is also sorted, by corresponding value (that is - names for values 0 go before names for values 1 etc):

var names = Enum.GetNames(typeof(FlipRotate2dEnum));

It also contains 16 names:

NO, None, R2, RotateTwice, ...

Then, binary search is performed over array of values for the given value. Say we call FlipRotate2dEnum.RotateNonClockwise.ToString(). It has value 7, so binary search is performed for 7:

var index = Array.BinarySearch(values, (byte)7)

Then, resulting index is used to get name:

var name = names[index];

Now, if you know how binary search works - you should now understand why results are not quite what you expect. With binary search values are not explored sequentially in the way they are defined, instead search starts in the middle and reduces search space in a half with every iteration, so it's not very obvious to which name your value is mapped while starring at enum declaration.

Of course you can achieve your desired result by just swapping RN and RotateNonClockwise, but I hope you understand from the above that it's not a great idea, not to mention you would rely on implementation detail, and addind\removing new members will be a pain.

As stated in comments - if you need "display" names for your enums - decorate them with some attribute and use it. Try to avoid enums with multiple names mapping to the same value, but if you do - at least don't think about that as one-to-one mapping between value and name, because that's not how it works.

3
  • So, binary search (in this case) looks like this : 0011223344556677 is byte values, 0000000100000000 - first step get second "3", 0001000000010000 - second step get second "1" or "5", 0100010001000100 - third step get second "0", "2", "4" or "6", 0000000000000010 - forth step get first "7", and that's why i see last name different ?
    – leofun01
    May 11, 2018 at 7:37
  • 1
    @leofun01 middle element is usually calculated as (low + high) / 2, where low is low index of search range (0 initially), and high is high index (length -1 initially, so 15). So first step uses second "3" indeed. It's smaller than target (7), so left half is throw away. Now low = 8, high = 15. Midpoint 11, that's second "5". And so on, eventually arriving at first "7", which index is the result.
    – Evk
    May 11, 2018 at 7:49
  • 1
    @leofun01 I didn't mention that explicitly, but for binary search to work - array should be sorted (and they are sorted). For that reason, if you see that midpoint is lower\higher than target - one half can be thrown away.
    – Evk
    May 11, 2018 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.