Enum.HasFlag, why no Enum.SetFlag?

Why isn’t there an Enum.SetFlag like there is an Enum.HasFlag?

HasFlag as a bitwise operation required more complicated logic and repeating the same flag twice

 myFlagsVariable=    ((myFlagsVariable & MyFlagsEnum.MyFlag) ==MyFlagsEnum.MyFlag );

so MS decided to implement it.

SetFlag and ClearFlag are concise in C#

    flags |= flag;// SetFlag

    flags &= ~flag; // ClearFlag 

but unfortunately not intuitive. Every time I need to set (or clear) a flag, I’m spending a few seconds (or minutes) to think: what is the name of the method? Why is it not shown in intellisense? Or no, I have to use bitwise operations. Note, that some developers will also ask: what is a bitwise operation?

Should SetFlag and ClearFlag extensions be created – YES to appear in intellisense.

Should SetFlag and ClearFlag extensions be used by developers – NO, because they are not efficient.

We’ve created extensions in our library’s class EnumFlagsHelper like in
SomeEnumHelperMethodsThatMakeDoingWhatYouWantEasier, but named the function as SetFlag instead of Include and ClearFlag instead of Remove.

In the body of SetFlag methods ( and in summary comment) I decided to add

Debug.Assert( false, " do not use the extension due to performance reason, use bitwise operation with the explanatory comment instead \n 
flags |= flag;// SetFlag")

and a similar message should be added to ClearFlag

Debug.Assert( false, " do not use the extension due to performance reason, use bitwise operation with the explanatory comment instead \n 
         flags &= ~flag; // ClearFlag  ")

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)