I was curious about C # enums and what happens to duplicate values. I created the following small program to check:
namespace ConsoleTest
{
enum TestEnum
{
FirstElement = -1,
SecondElement,
ThirdElement,
Duplicate = FirstElement
}
public class MainConsole
{
public MainConsole()
{
}
public static void Main(string[] args)
{
TestEnum first = TestEnum.FirstElement;
TestEnum second = TestEnum.SecondElement;
TestEnum duplicate = TestEnum.Duplicate;
foreach (string str in Enum.GetNames(typeof(TestEnum)))
{
Console.WriteLine("Name is: " + str);
}
Console.WriteLine("first string is: " + first.ToString());
Console.WriteLine("value is: " + ((int)first).ToString());
Console.WriteLine("second string is: " + second.ToString());
Console.WriteLine("value is: " + ((int)second).ToString());
Console.WriteLine("duplicate string is: " + duplicate.ToString());
Console.WriteLine("value is: " + ((int)duplicate).ToString());
TestEnum fromStr = (TestEnum)Enum.Parse(typeof(TestEnum), "duplicate", true);
Console.WriteLine("fromstr string is: " + fromStr.ToString());
Console.WriteLine("value is: " + ((int)fromStr).ToString());
if (fromStr == TestEnum.Duplicate)
{
Console.WriteLine("Duplicate compares the same as FirstElement");
}
else
{
Console.WriteLine("Duplicate does NOT compare the same as FirstElement");
}
}
}
}
Which produces the following output:
Name is: SecondElement
Name is: ThirdElement
Name is: FirstElement
Name is: Duplicate
first string is: FirstElement
value is: -1
second string is: SecondElement
value is: 0
duplicate string is: FirstElement
value is: -1
fromstr string is: FirstElement
value is: -1
Duplicate compares the same as FirstElement
Press any key to continue . . .
This seems to be EXACTLY what I want and expect, since I build that the version tag will grow every so often, so I want something that I can “assign” to the current version and even compare with it.
Here's the question though: what are the pitfalls of this approach? Is there any? Is this just a bad style (I don't want to end up on thedailywtf)? Is there a better way to do something like this? I am on .NET 2.0 and have no way to upgrade to 3.5 or 4.0.
Opinions are welcome.