C # Enumerations and Duplicate Values ​​- Dangers?

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
    }

    /// <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 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.

+5
3

, , . "Duplicate", , 0, .

+1

, . .

[Flags]
enum Rows {
  Even = 0x01,
  Odd = = 0x02,
  All = Even | Odd
}

, Cwalina Abrams, :

(, , ..).

.

.

, , , . Microsoft , , .

+5

I do not see anything wrong with this, and in fact sometimes I do the same. There are times when a method (especially the native P / Invoke for APi that has grown over time) can take the same numerical values, but they mean something else, so I like to have an enumeration variable name that describes usage.

+3
source

All Articles