I was wondering about C# Enumerations and what happens with duplicate values. I created the following small program to test things out:
namespace ConsoleTest
{
enum TestEnum
{
FirstElement = -1,
SecondElement,
ThirdElement,
Duplicate = FirstElement
}
/// <summary>
/// Summary description for MainConsole.
/// </summary>
public class MainConsole
{
/// <summary>
/// Constructor for the class.
/// </summary>
public MainConsole()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// Entry point for the application.
/// </summary>
/// <param name="args">Arguments to the application</param>
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'm constructing something that a version tag will increment every so often, so I want something that I can "assign" to the current version, and even compare to it.
Here's the question though: what's the pitfalls of this approach? Is there one? Is it just bad style (I don't want to end up on thedailywtf)? Is there a lot better way of doing something like this? I'm on .NET 2.0 and do NOT have the option to go to 3.5 or 4.0.
Opinions are welcome.