Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-unique enum values

Tags:

c#

enums

struct

I am trying to obscure the index positions on an edi file... I had a situation where 2 or 3 things could be at an index based on the situation. It'd be cool to use an enum to hide the "magic numbers" and was suprised to see that you could assign multiple enums to the same value like this:

public enum Color
{
    Red = 1,
    Blue = 1,
    Green = 1
}

and the compiler is happy with this. I didn't expect this to work. I don't need to cast back to the enum so I'm not worried about trying to go back, but this smells funky. Why does the CLR allow multiple values for enums and should I use a struct for this? (A struct seemed heavier duty than an enum and this seems to work)

like image 639
Rikon Avatar asked Nov 07 '11 21:11

Rikon


People also ask

Can enum elements have same value?

Since there is no problem with a type that contains an multiple constants that have the same value, there is no problem with the enum definition. But since the enum does not have unique values you might have an issue when converting into this enum.

Do enums have to be unique?

Enum names are in global scope, they need to be unique.

Can two enum values be the same?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can enum have duplicate values in C#?

Enums can not have duplicate values.


3 Answers

Actually you're already defining a struct... Behind the scenes an enum is just a struct (but which derives from System.Enum) and the values of the enum are defined as constants (you can verify this with ILDASM).

Your enum definition translates into the following pseudo C# code:

public struct Color : System.Enum {     public const int Red = 1;     public const int Blue = 1;     public const int Green = 1; } 

The above code won't compile in C# because the compiler doesn't allow defining a struct with an explicit base class, but that's what it emits for an enum definition.

Since there is no problem with a type that contains an multiple constants that have the same value, there is no problem with the enum definition.

But since the enum does not have unique values you might have an issue when converting into this enum. For example the following two line of codes will return the enum value Red, because the first value is arbitrarily selected.

Color color1 = (Color)1; Color color2 = (Color)Enum.Parse(typeof(Color), "1"); 

Strictly speaking the enum value is not Red, it is 1, but when you print out the value you'll see Red.

Also, the following boolean is true which looks a bit weird...

// true (Red is Green??) bool b = Color.Red == Color.Green; 

At the bottom line this is perfectly legal, but it's up to you to use it when it makes sense...

Here is a direct link to the section of my .NET tutorial that discusses enumerations under the hood: http://motti.me/c1E

like image 195
Motti Shaked Avatar answered Oct 07 '22 03:10

Motti Shaked


The same numeric value but different name is nothing else as an alias. It could be e.g.

public enum Color
{
   DefaultColor = 1,
   Red = 1,
   Blue = 2
}

It can make sense in some cases but not many. When you parse the values back and call colorValue.ToString() you will get the last value as stringified value back (Red in this case) but you will loose the conept of default colors since it is the same thing. At least in the way you did model your data. If you want to keep it separate use different values for different things.

like image 28
Alois Kraus Avatar answered Oct 07 '22 03:10

Alois Kraus


That's perfectly legal C#. From the C# Language specification version 4.0, section 14.3:

Multiple enum members may share the same associated value. The example

enum Color 
{
   Red,
   Green,
   Blue,
   Max = Blue
}

shows an enum in which two enum members—Blue and Max—have the same associated value.

like image 43
Mark Byers Avatar answered Oct 07 '22 02:10

Mark Byers