14

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
    }
theMayer
  • 15,456
  • 7
  • 58
  • 90
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • 11
    What did the compiler say when you tried it? Seriously, does no-one ever _think_ anymore? – paxdiablo Mar 17 '13 at 06:18
  • 3
    @paxdiablo In C++ a lot of undefined behavior will be accepted by compilers without warning. – ta.speot.is Mar 17 '13 at 06:21
  • 5
    @ta.speot.is, this isn't C++, it's C#. The behaviour is defined by MS. – paxdiablo Mar 17 '13 at 06: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. – theMayer Mar 17 '13 at 06:23
  • @paxdiablo The question is tagged VB .NET – ta.speot.is Mar 17 '13 at 06:28
  • @ta.speot.is Which is also not C++. He is probably more interested in whether this works in CLR languages. – John Colanduoni Mar 17 '13 at 06:29
  • @ta.speot.is: and, on top of that, it's _also_ tagged c#, and the code isn't vb, not by a long stretch :-) – paxdiablo Mar 17 '13 at 06:32
  • @GeorgeWBush I cant see the link, but if you say so I will take more care, OP post [another duplicate shortly before](http://stackoverflow.com/questions/15458257/how-to-have-enum-values-with-spaces). Guess I jumped to conclusion. – Jeremy Thompson Mar 17 '13 at 12:02

4 Answers4

22

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Interestingly, if you have the code analyzer [CA1069](https://learn.microsoft.com/en-au/visualstudio/code-quality/ca1069?view=vs-2019) enabled, the above is fine, but if you assigned `Max = 2` then the analyzer will complain about assigning the same constant value – David Gardiner May 26 '20 at 03:47
  • 2
    @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. – paxdiablo May 26 '20 at 03:50
  • 1
    I think you're right. It's not a bad way of checking you're doing the right thing – David Gardiner May 26 '20 at 05:36
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
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
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.

Community
  • 1
  • 1
theMayer
  • 15,456
  • 7
  • 58
  • 90
-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" }
        }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331