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?
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 ofDialogResults.Try supplying a
HashSetof the items you allow.HashSet<T>implementsISet<T>, and it’s usually best to work against interfaces than concrete types, especially for method signatures:Then you can use
Containsto test for items: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.