How to prevent duplicate values in enum?
Unit test that checks enum and shows which particular enum values has duplicates:
[Fact]
public void MyEnumTest()
{
var values = (MyEnum[])Enum.GetValues(typeof(MyEnum));
var duplicateValues = values.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key).ToArray();
Assert.True(duplicateValues.Length == 0, "MyEnum has duplicate values for: " + string.Join(", ", duplicateValues));
}
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.
Here's a simple unit test that checks it, should be a bit faster:
[TestMethod]
public void Test()
{
var enums = (myEnum[])Enum.GetValues(typeof(myEnum));
Assert.IsTrue(enums.Count() == enums.Distinct().Count());
}