I wonder is there a way to prevent an enum
with duplicate keys to compile?
For instance this enum
below will compile
public enum EDuplicates
{
Unique,
Duplicate = 0,
Keys = 1,
Compilation = 1
}
Although this code
Console.WriteLine(EDuplicates.Unique);
Console.WriteLine(EDuplicates.Duplicate);
Console.WriteLine(EDuplicates.Keys);
Console.WriteLine(EDuplicates.Compilation);
Will print
Duplicate
Duplicate
Keys
Keys
This isn't prohibited by the language specification, so any conformant C# compiler should allow it. You could always adapt the Mono compiler to forbid it - but frankly it would be simpler to write a unit test to scan your assemblies for enums and enforce it that way.