根据上下文标记具有共享值的枚举?

发布于 2024-12-27 03:35:06 字数 310 浏览 1 评论 0原文

我想设计一个标志枚举,但由两个用例共享:

  • 用例 1:存储是/否答案。
  • 用户案例2:存储多项选择答案。

例如: 我定义了以下内容

[Flags]
public enum Answer
{
    Yes = 1, 
    No = 2,
    Choice1 = 1,
    Choice2 = 2,
    Choice3 = 4,
    Choice4 = 8,
}

组合它的原因是共享一个一致的接口(例如过程答案)。对于这个设计有什么异议吗? 谢谢!

I want design a flag enum but shared by two use cases:

  • Use case 1: to store yes/no answer.
  • User case 2: to store multiple choice answer.

For example: I have defined the following

[Flags]
public enum Answer
{
    Yes = 1, 
    No = 2,
    Choice1 = 1,
    Choice2 = 2,
    Choice3 = 4,
    Choice4 = 8,
}

The reason to combine this is to share a consistent interface(e.g. process answer). Are there any arguments against this design?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尬尬 2025-01-03 03:35:06
[Flags]
public enum Answer
{
    No = 0, 
    Choice1 = 1,
    Choice2 = 2,
    Choice3 = 4,
    Choice4 = 8,
    Yes = 0x80000000,
}
[Flags]
public enum Answer
{
    No = 0, 
    Choice1 = 1,
    Choice2 = 2,
    Choice3 = 4,
    Choice4 = 8,
    Yes = 0x80000000,
}
想挽留 2025-01-03 03:35:06

我不会这样做。原因如下:

class Program
    {
        [Flags]
        public enum Answer
        {
            Yes = 1,
            No = 2,
            Choice1 = 1,
            Choice2 = 2,
            Choice3 = 4,
            Choice4 = 8,
        }

        static void Main(string[] args)
        {

            int SomeInt = (int)Answer.Choice1;

            Console.WriteLine((Answer)SomeInt);

            Console.ReadKey();
        }
    }

结果是

“是”

这是有道理的,而且显然它在语法上是正确的,但是你让维护这样的枚举变得非常困难。它将匹配枚举的第一个正确值。我相信这是可以改进的逻辑,从而限制/消除混乱。

我会这样做吗?

I wouldn't do this. Here's why:

class Program
    {
        [Flags]
        public enum Answer
        {
            Yes = 1,
            No = 2,
            Choice1 = 1,
            Choice2 = 2,
            Choice3 = 4,
            Choice4 = 8,
        }

        static void Main(string[] args)
        {

            int SomeInt = (int)Answer.Choice1;

            Console.WriteLine((Answer)SomeInt);

            Console.ReadKey();
        }
    }

The result of that is

"Yes"

It makes sense, and obviously it is syntactically correct, but you are making it very hard to maintain an enumeration like that. It is going to match on the first correct value of the enumeration. That is logic that I believe can be improved upon so that confusion is limited/eliminated.

Would I do this? No.

清风挽心 2025-01-03 03:35:06

1)如果你想检查问题是否被回答,那么你可以检查值1(如果值1表示没有回答)。
2) 如果设置了任何选项,那么您可以假设您的答案正在被回答。

public enum Answer {
  No = 1,     
  Choice1 = 2,     
  Choice2 = 3,     
  Choice3 = 4,     
  Choice4 = 8
} 

希望这有帮助!

1) If you want to check whether the question is being answered or not then you can check for value 1 (if the value 1 Means it is not answered).
2) If any of the choices is set then you can assume that your answered is being answered.

public enum Answer {
  No = 1,     
  Choice1 = 2,     
  Choice2 = 3,     
  Choice3 = 4,     
  Choice4 = 8
} 

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文