Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Which is the currently-accepted best practice in terms of C# enum usage, a bitwise [Flags] Enum (compared using | and &) or an IEnumerable of enum values? I'm thinking both from a code-conciseness perspective and from a maintenance perspective.

The only downside to using the Flags approach that I can think of is the inability to enumerate the values without doing the reverse enumeration:

var possibleValues = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>();
var applicableValues = possibleValues.Where(x => (containerClass.EnumProperty & x) == x);

Whereas obviously the IEnumerable is easier to query.

EDIT:

As svick rightly pointed out, the IEnumerable would be perhaps the wrong collection to use, an ISet might be a better example.

share|improve this question
    
What would be the purpose of an IEnumerable of enum values? What do you intend to use the Enum for? –  Oded Dec 20 '11 at 11:58
    
To hold a collection of attributes about the container class (the concrete example being attributes about a product, such as "Price Marked" (i.e. has the price printed on the pack)). The options are pre-set, making it a good case for an enum. –  Ed Woodcock Dec 20 '11 at 12:01
add comment

1 Answer

You can create a method that encapsulates enumerating the flags. This way, using flags is almost as easy as an IEnumerable<T>.

One reason why I would prefer flags is what those two approaches mean. With flags, you have a set. With IEnumerable<T>, you have a sequence. That means you have another edge case you have to consider: What if the sequence contains some item more than once?

share|improve this answer
    
Yes, I'd definitely not repeat the code I posted multiple times (it would work nicely as an extension method). I'm not sure the "more than once" argument is more than an aside, however, as in the situation I described, i.e. dealing with attributes, it would simply not be relevant if the number of matches was 1 or 1000, it would just matter if the attribute was present. Data integrity is another issue ;) Perhaps an ISet<Enum> would suffice to negate your example? –  Ed Woodcock Dec 20 '11 at 12:42
    
@EdWoodcock, except that there is no ISet<T> in the .Net library. –  svick Dec 20 '11 at 13:03
1  
See: msdn.microsoft.com/en-us/library/dd412081.aspx . It was added in .net 4 I believe. –  Ed Woodcock Dec 20 '11 at 13:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.