
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
28
|
Chapter 1: Numbers and Enumerations
Combining elements of this enumeration is a simple matter of using the bitwise OR
operator (
|). For example:
Language lang = Language.CSharp | Language.VBNET;
Discussion
Adding the Flags attribute to an enumeration marks this enumeration as individual
bit flags that can be ORed together. Using an enumeration of flags is no different
than using a regular enumeration type. It should be noted that failing to mark an
enumeration with the
Flags attribute will not generate an exception or a compile-
time error, even if the enumeration values are used as bit flags.
The addition of the
Flags attribute provides you with two benefits. First, if the Flags
attribute is placed on an enumeration, the ToString and ToString("G") methods
return a string consisting of the name of the constant(s) separated by commas. Oth-
erwise, these two methods return the numeric representation of the enumeration
value. Note that the
ToString("F") method returns a string consisting of the name of
the constant(s) separated by commas, regardless of whether this enumeration is
marked with the
Flags attribute. For an indication of why this works in this manner,
see the
"F" formatting type in Table 1-2 in Recipe 1.16.
The second benefit is that when you examine the code and encounter an