C# 枚举和重复值 - 危险?

标签 c# .net-2.0 enums

我想知道 C# 枚举以及重复值会发生什么。我创建了以下小程序来测试:

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");
            }

        }
    }
}

产生以下输出:

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

这似乎正是我想要和期望的,因为我正在构建一个版本标签会经常递增的东西,所以我想要一些我可以“分配”给当前版本的东西,甚至可以与它进行比较。

但问题是:这种方法的缺陷是什么?有吗?只是风格不好(我不想在 thedailywtf 上结束)?有没有更好的方法来做这样的事情?我使用的是 .NET 2.0,无法选择转到 3.5 或 4.0。

欢迎提出意见。

最佳答案

如果您有标志枚举,则提供别名枚举值会很有用。例如

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

但是,如果您阅读 Cwalina 和 Abrams 编写的 Framework Design Guidelines 中的枚举指南,您会发现以下建议:

DO NOT use an enum for open sets (such as the operating system version, names of your friends etc.).

DO NOT include sentinel values in enums.

拥有当前版本是一个标记值。

这些准则适用于框架设计,如果您只想在应用程序内部使用枚举,那么即使违反这些准则也没有关系。但是,Microsoft 制定了这些准则,以避免在其他开发人员必须使用您的代码时出现常见陷阱。

关于C# 枚举和重复值 - 危险?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1208247/

相关文章:

c# - 事件订阅者是否按订阅顺序调用?

c# - 这两个构造函数做同样的事情吗?

c# - CA1019 : Define accessor for attribute argument. 不明白是什么原因

c# - 这个通用继承是如何完成的(内部)?

c# - 从 C# 中的字符串数组中删除重复字符串的有效方法

c# - 在 Java 中从 C# 转换标志

ios - 串联枚举返回类型

python - 覆盖扩展 Enum 的类的 __new__

c# - 无法为对象设置自定义鉴别器约定

c# - 如何在不同的命名空间中处理相同的类名?