问题
Assume i have an enumeration:
namespace System.Windows.Forms
{
public enum DialogResult { None, OK, Cancel, Abort, Retry, Ignore, Yes, No }
}
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;
And then i can use
ShowForm(frm, DialogResult.OK | DialogResult.Retry);
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.
- in one case the type should only allow one value (
DialogResult) - in another case the type should allow multiple values of above (
DialogResults)
How can i have a "set" of enumerated types?
Note: i assume it's not possible in C#. If that's the answer: it's okay to say so - the question is answered.
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.
Update: another example:
Assume i have an enumeration:
public enum PatronTier
{
Gold = 1,
Platinum = 2,
Diamond = 3,
SevenStar = 7 //Yes, seven
}
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;
And then i can use:
tournament.EligibleTiers = PatronTier.Gold | PatronTier.SevenStar;
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.
How can i have a "set" of enumerated types?
回答1:
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.
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))
{
}
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.
回答2:
I don't suppose you just mean using something like this?
var DialogResults = Enum.GetValues(typeof(DialogResult));
with a .Select(dr => (DialogResult)dr).ToArray() if you want it strongly typed.
回答3:
I think you want something like this:
foreach (var item in System.Enum.GetValues(typeof(PatronTier)))
{
Console.WriteLine(item);
}
来源:https://stackoverflow.com/questions/8523544/how-to-have-a-set-of-enumeration-values-in-c