0

I was under the impression that the int values behind enums were unique. Therefore if you assign the ints manually and accidentally duplicate a value then it should fail at compile time.

Why does the following code compile ok and is there a way to force this to fail in order to prevent accidental duplicaiton?

    class Program
{
    static void Main(string[] args)
    {
        Test a = Test.A;
    }
}

enum Test
{
    A = 1,
    B = 1,
}
  • 1
    Enums are effectively just named constants, they do not have to be unique : http://stackoverflow.com/questions/8043027/non-unique-enum-values. Here are some ways of checking for duplicate values : http://stackoverflow.com/questions/1425777/how-to-prevent-duplicate-values-in-enum – PaulF Sep 02 '15 at 15:50
  • 2
    Accidentally closed as a duplicate of a duplicate, meant to close as a duplicate of the original. Sorry about that. – Brandon Sep 02 '15 at 15:50
  • The integers don't have to be unique. Only the keys have to be unique – rmn36 Sep 02 '15 at 15:50

0 Answers0