This is wrong approach. Instead, use the enumeration type, each enumeration member integer value assigned to 1, 2, 4, etc. You will be able to perform binary arithmetic on enumeration members as they were simple integers. Now, if you also add a
[Flags]
attribute to such type, the method
ToString
will simply output the string with several comma-separated names of enumeration value, representing the ORed result of the values. Please see:
http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx[
^].
And if you need more detail or want more advanced techniques of working with enumerations, especially those representing bit sets, please read two of my articles where I describe the related problem in detail, and describe the standard techniques and also provide some advanced techniques, such as traversing the set of enumeration members (enumerating them, which is quite amazingly, is not directly supported by enumeration types), customized string representation of enumeration values, and more:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^],
Human-readable Enumeration Meta-data[
^].
—SA