15

In .NET can you have multiple enum values for the same integer?

eg.

public enum PersonGender
    {
        Unknown = 0,
        Male = 1,
        Female = 2,
        Intersex = 3,
        Indeterminate = 3,
        NonStated = 9,
        InadequatelyDescribed = 9
    }
8
  • 11
    What did the compiler say when you tried it? Seriously, does no-one ever think anymore? Commented Mar 17, 2013 at 6:18
  • 4
    @paxdiablo In C++ a lot of undefined behavior will be accepted by compilers without warning. Commented Mar 17, 2013 at 6:21
  • 5
    @ta.speot.is, this isn't C++, it's C#. The behaviour is defined by MS. Commented Mar 17, 2013 at 6:22
  • 1
    I'm curious to know, what is your underlying purpose in this? Obviously, what you say want is illogical; however, it doesn't seem like you would ask a question whose ultimate purpose makes no sense. Commented Mar 17, 2013 at 6:23
  • @paxdiablo The question is tagged VB .NET Commented Mar 17, 2013 at 6:28

4 Answers 4

24

In C#, this is allowed, as per the C# Language Specication, version 4. Section 1.10 Enums doesn't explicitly mention the possibility but, later on in section 14 Enums, 14.3, we see:

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Interestingly, if you have the code analyzer CA1069 enabled, the above is fine, but if you assigned Max = 2 then the analyzer will complain about assigning the same constant value
@David, it's probably assuming that, if you assign a constant to two different enum values, there's a non-zero chance that you may have made a mistake, especially if the values are all over the place: enum x { a = 42, b = 17, c = 12, d = 314159, e = 271828, f = 17 };. If you assign an already-existing enum value to an enum value, it's probably safe to assume you meant to do that.
I think you're right. It's not a bad way of checking you're doing the right thing
4

That works fine. There is absolutely nothing wrong with the code you posted. It compiles just fine and works in code, with the caveat that

PersonGender.NonStated == PersonGender.InadequatelyDescribed

Comments

2

I found this StackOverflow post related to this question. I think there is a very sensible discussion of how this works. Non-unique enum values

Now, I might also add that it is my opinion this would be an ambiguous (and therefore improper) use of an enum. It's important to write code that makes sense to someone else reading it, and in this case, I would be put off by this enum.

Comments

-1

I would suggest that an enum would not be a right thing to use in your context instead you can use create a class and methods which can resolve your purpose. Something like this in your class:-

class A
{

    static readonly ABCD= new Dictionary<int,string>
        {
            { 1, "X" },
            { 2, "X" },
            { 3, "Y" }
            { 4, "Y" }
        }
}

2 Comments

How did you come to that conclusion?
@MichaelPetrotta:- Yes I agree that I should not give it is as a conclusion but definitely thats my suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.