如何在C#中有一组枚举值? [英] How to have a set of enumeration values in C#?

查看:161
本文介绍了如何在C#中有一组枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有枚举

namespace System.Windows.Forms
{
   public enum DialogResult { None, OK, Cancel, Abort, Retry, Ignore, Yes, No }
}

我想声明一个>set由这些枚举类型组成

i want to declare a "set" made up of these enumerated types

ShowForm(Form frm, DialogResults allowedResults)

在其他语言中,您将声明:

In other languages you would declare:

public DialogResults = set of DialogResult;

然后我可以使用

ShowForm(frm, DialogResult.OK | DialogResult.Retry);

C#具有 标志 的概念,伪代码:

C# has the notion of Flags, pseudocode:

[Flags]
public enum DialogResults { DialogResult.None, DialogResult.OK, DialogResult.Cancel, DialogResult.Abort, DialogResult.Retry, DialogResult.Ignore, DialogResult.Yes, DialogResult.No }

这不是真正的代码 - 标志不会指示编译器创建一组标志。

problem with that it's not real code - Flags does not instruct the compiler to create a set of flags.


  • 在某种情况下,该类型应该只允许另一个一个值( DialogResult

  • 情况下,该类型应允许多个值( DialogResults

  • in one case the type should only allow one value (DialogResult)
  • in another case the type should allow multiple values of above (DialogResults)

我有一个枚举类型的set


注意:我假设在C#中是不可能的。如果是这样的答案:可以这么说 - 问题得到回答。

Note: i assume it's not possible in C#. If that's the answer: it's okay to say so - the question is answered.

注意:只是因为我相信C#语言没有这个功能并不意味着没有这个功能 - 我可以只是没有找到它。

Note: Just because i believe C# language doesn't have the feature doesn't mean it doesn't have the feature - i may just not have found it yet.






更新:另一个例子:

假设我有一个枚举:

public enum PatronTier 
{ 
    Gold = 1,      
    Platinum = 2,  
    Diamond = 3,   
    SevenStar = 7  //Yes, seven
}

我想声明由这些枚举类型组成的set >

i want to declare a "set" made up of these enumerated types

public Tournament
{
   public PatronTiers EligibleTiers { get; set; }
}

在其他语言中,您将声明:

In other languages you would declare:

public PatronTiers = set of PatronTier;

然后我可以使用:

tournament.EligibleTiers = PatronTier.Gold | PatronTier.SevenStar;

C#具有 标志 的概念,伪代码:

C# has the notion of Flags, pseudocode:

[Flags]
public enum PatronTiers { PatronTier.Gold, PatronTier.Platinum, PatronTier.Diamond, PatronTier.SevenStar }

该问题不是真正的代码。

problem with that it's not real code.

我如何拥有枚举类型的set

How can i have a "set" of enumerated types?

推荐答案

像你想要一系列的东西。在C#中有数组类型,但直接 相当于编译器支持的例子,最接近的就是 DialogResults [] 数组 DialogResults

Seems like you want an array of things. There are array types in C#, but nothing that is directly equivalent to your examples in terms of compiler support, closest is perhaps DialogResults[], an array of DialogResults.

尝试提供一个 HashSet 你允许的项目 HashSet< T> 实现 ISet< T> ,通常最好是针对接口而不是具体类型,特别是方法签名:

Try supplying a HashSet of the items you allow. HashSet<T> implements ISet<T>, and it's usually best to work against interfaces than concrete types, especially for method signatures:

ShowForm(Form frm, ISet<DialogResults> allowedResults);

然后,您可以使用包含来测试项目:

Then you can use Contains to test for items:

if (allowedResults.Contains(DialogResults.OK))
{
}

有些无意义的选择:,您可以随时实现自己的 Set< ;枚举> 使用 Jon Skeet的无约束旋律进行输入你可以从调用者的角度来看一个更好的语法,并且更接近你的例子。

Somewhat pointless alternative: you could always implement your own Set<Enum> type using Jon Skeet's Unconstrained Melody to give you a nicer syntax from the perspective of the caller and get a little closer to your examples.

这篇关于如何在C#中有一组枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆