aAbEBABDBEBrrreX-~| 76565454VC~W 766464VCC 697VBXC~BZZZC~ 66466476666566VCCP7-~B-~B~C 76666G7656566V~~B~ 764VBCC 9V 7F5 7ZZZX-

Enum.` In the forest a flower grows. This flower has many attributes. It is blooming. It is pink (not blue or yellow). A gentle wind passes through the region.`With an enum,` we can classify this plant. Consider that flowers come in all colors. It could use FlowerColor.Pink. Another one could be Blue.`Enum example.` Here is an enum that expresses importance. An enum type internally contains an enumerator list. The values (like Trivial and Critical) are ints like 1 and 4. `Int: `The underlying value of this enum is the default, which is int. When we use an Importance variable, we are using an int.`Enum advantages.` With an enum, magic constants are separate. This modular design makes things easier to understand. Fewer bugs will be introduced. `Enums can be used with IntelliSense in Visual Studio. This feature will guess the value you are typing.`Press tab: `We can simply press tab and select the enum type we want. This is an advantage to using enum types.`Debugger.` Next, we examine what enums look like in the Visual Studio debugger. We see that enums are strongly-typed. You cannot assign them to just any value. `The debugger shows that en1 and en2 are of type Program.E. But internally, these two variables are stored as integers.`Strings.` We convert enums to strings for display on the Console. Enum values always have a name, such as E.None, E.BoldTag or E.ItalicsTag. `To print out the enum values, you can call ToString on the enum variable in a program.`Another method such as Console.WriteLine can automatically call the ToString method.`ToString.` Console.WriteLine will call the ToString method on all types passed to it. Internally, ToString invokes methods that use reflection to acquire the string representation. `Enum ToString `enum-tostring`A warning.` Some examples here use short, letter-based identifiers (b1, v2) for variables. These are not ideal. It is better to use more descriptive words, such as "animal" or "visibility."`Parse.` Sometimes we have a string value that we want to convert to an equivalent enum. This could happen when accepting user input. `When using the .NET Framework, calling a built-in method to do conversions (where one exists) is usually best.`Enum.Parse: `The tricky part of using this method involves typeof and casting. It is best to avoid this if possible.`Enum.Parse `enum-parse`GetName, Getnames.` Built-in methods get strings that represent enums. With GetName, we can get the name for an enum value. With GetNames we get all the string representations at once. `Enum.GetName `enum-getname`Format enums.` It is possible to format the values stored in enums in different ways. We can display an integer representation, or a hex representation. `Enum.Format `enum-format`Switch.` The above samples show if-statements used with enums. But switch in the C# language is sometimes compiled to more efficient IL. `Switch Enum `switch-enum`The IsFormat method works as a filter that tells us something about sets of enum values.`Logic: `We can separate the logic here instead of repeating ourselves. This helps clarify the program logic.`Default.` Values are always initialized to zero when they are fields of a class. Upon class creation, an enum field will also be initialized to zero (and the equivalent value). `To make enums valid, always use the default value of zero. This way, we can test for the default value of fields.`Sometimes: `This issue is not worth fixing. But it is often useful for verifying correctness.`FxCop.` This is a code analysis tool released by Microsoft. It helps us improve certain issues in code. It will tell us that "enums should have zero value." `Quote: `A non-flags attributed enumeration should define a member with the value of zero so that the default value is a valid value of the enumeration. If appropriate, name the member None.`Enums should have zero value: MSDN `https://msdn.microsoft.com/en-us/library/ms182149(VS.80).aspx`Collections.` Here we apply the Stack collection in the .NET Framework. With Stack, we can develop a parser that keeps the most recently encountered enum value on the top. `Stack: `The Stack here can only have E-type values added to it. This is an example of type checking and validation.`Stack `stack`Pop: `With the Pop method we get the top element from the stack. This is of type E.ItalicsTag.`Result: `In the execution of this program, the stack has two enums added and one removed.`Type.` An enum has an underlying type. Each time we use the enum, we are using the underlying type. The enum has syntactic sugar on top. `Int: `Enums are by default an int type, but we can adjust this to a different numeric type.`Byte: `Here we create an enum with a type of byte. This is sometimes useful on small enums. A byte can only contain 256 different values.`Byte `byte`Memory: `The CoffeeSize enum will use memory equivalent to a byte. This can make classes more efficient and smaller.`GetUnderlyingType.` We can determine an enum's type (like int) at runtime. Enum.GetUnderlyingType, a static method, determines the underlying type. `Static Method `static`Next: `We declare an enum Importance. For this example it uses an underlying type of byte.`Then: `When the GetUnderlyingType method is called, the System.Byte type is returned.`Null, none.` An enum value cannot be null. It is a value type like an int. To avoid the "cannot convert null" error, use a special None constant as the first enum item. `Flags.` The language allows us to specify a Flags attribute on an enum. This enables the enum to be used as a bit field. We can use combinations of enum values this way. `Flags `enum-flags`Arrays.` Enums are values. We can use enums to index arrays. This approach is useful for some kinds of tables or data structures in programs. `Enum Array `enum-array`Performance.` Enums are fast. They are almost never a performance concern. They are just syntactic sugar on a type like int, which is also fast. `Enum Performance `enum-performance`Memory.` Suppose we develop a class that has an enum field. The underlying type of this enum contributes to how much memory the class will use.`Memory, continued.` A smaller type (such as byte) will make class instances that hold the enum smaller. Thousands of class instances are required before this optimization is important.`A review.` Enums enhance clarity and reduce the probability of invalid constants. We use them to represent constant values (such as integers) in a consistent way.

OYxXKxxYDYQYbYJYAx{xOKenumK ImportancexO{xOONone,xOOTrivial,xOORegular,xOOImportant,xOOCriticalxO}YbOY%O{KxOOY9An enum local variable.xOOKImportance YhYyKImportance.CriticalK;KxxOOY9Test against known Importance Yhs.xOOKYmYhYxKImportance.TrivialK)xOO{xOOOY'KXNot trueXK);xOO}xOOelse YmYhYxKImportance.CriticalK)xOO{xOOOY'KXTrueXK);xOO}xO}x}xxKxxTrueKxxYDYQYbYJYAx{xOKenumK ExO{xOONone,xOOBoldTag,xOOItalicsTag,xOOHyperlinkTag,xO}YbOY%O{KxOOY9These Yhs are enum E types.xOOKE en1YyE.BoldTag;xOOE en2YyE.ItalicsTagYbOOYmen1YxE.BoldTag)xOO{KxOOOY{Will be Y[ed.xOOOKY'KXBoldXK);xOO}xOOYmen1YxE.HyperlinkTag)xOO{KxOOOY{Won't be Y[ed.xOOOKY'KXNot trueXK);xOO}xO}x}xxKxxBoldKxxYDYQYbYJYAx{xOY%O{KxOOY9Two enum variables.xOOKB b1YyB.Dog;xOOV v1YyV.Hidden;KxxOOY9PrYiout their Yhs.xOOKY'b1);xOOY'v1);xO}xxOKenumK VxO{xOONone,xOOHiddenYy2,xOOVisibleYy4xO}YbOKenumK BxO{xOONone,xOOCatYy1,xOODogYy2xO};x}xxKxxDogxHiddenKxxYDYQYbYJYAx{xOY%O{KxOOY9Test enum with YH mYg.xOOKG e1YyG.None;xOOYmIsFormat(e1))xOO{KxOOOY{Won't succeed.xOOOY9G.None is not a Y|mat Yh.xOOOKY'KXErrorXK);xOO}KxxOOY9Test another enum with YH.xOOKG e2YyG.ItalicsFormat;xOOYmIsFormat(e2))xOO{KxOOOY{Will succeed.xOOOY9G.ItalicsFormat is a Y|mat Yh.xOOOKY'KXTrueXK);xOO}xO}xxOKenumK GxO{xOONone,xOOBoldFormat,KOY{Is a Y|mat Yh.xOOKItalicsFormat,K Y{Is a Y|mat Yh.xOOKHyperlinkKO Y{Not a Y|mat Yh.xOK};KxxO/Y{<Y7>xO/Y{Returns true if the G enum Yh is a Y|mat Yh.xO/Y{</Y7>xOKYBY?bool IsFormat(G e)xO{xOOKYHK (e)xOO{xOOOYfG.BoldFormat:xOOOYfG.ItalicsFormat:xOOOO{KxOOOOOY{These two Yhs are Y|mat Yhs.xOOOOOKYK true;xOOOO}xOOOdefault:xOOOO{KxOOOOOY{The argument is not a Y|mat Yh.xOOOOOKYK false;xOOOO}xOO}xO}x}xxKxxTrueKEnum with default Yh of NoneKxxKenumK Ex{xONone,xOA,xOB,xOCx};KxxYDYQ;xY xYJYAx{xOY%O{xOOM();xO}xxOKenumK ExO{xOONone,KOO Y{Yzeger YhYy0xOOKBoldTag,KO Y{1xOOKItalicsTag,K Y{2xOOKHyperlinkTag,K Y{3xOK}YbOY?YBYcM()xO{KxOOY9Stack of enums.xOOKStackK<E> stackYyYqStack<E>();KxxOOY9Add enum YhsYjthe Stack.xOOKstack.Push(KE.BoldTagK);K Y{Add bold.xOOKstack.Push(KE.ItalicsTagK);K Y{Add italics.xxOOY9Ykthe top enum Yh.xOOKE thisTagYystack.Pop();K Y{Yktop tag.xOOKY'thisTag);xO}x}xxKxxItalicsTagKxxYDYQYbYJYAx{xOenum CoffeeSize : KbyteKxO{xOONone,xOOTall,xOOVenti,xOOGrandexO}YbOY%O{KxOOY9CY_ a coffee size local.xOOKCoffeeSize sizeYyKCoffeeSize.VentiK;xOOY'size);xO}x}xxKxxVentiKxxYDYQYbYJYAx{xOenum Importance : bytexO{xOOLow,xOOMedium,xOOHighxO}YbOY%O{KxOOY{Determine the underlying type of the enum.xOOKType typeYyKEnum.GetUnderlyingTypeK(typeof(Importance));xOOY'type);xO}x}xxKxxYQ.ByteKxxenum Colorx{xONone,xOBlue,xORedx}xxYJYAx{xOY%O{xOOColor cYyKnullK;xO}x}xxKResultsKxxKError CS0037KxCannot cYN nullYj'Color' because it is a non-nullable Yh typexxKxxenum Colorx{xONone,xOBlue,xORedx}xxYJYAx{xOY%O{xOOColor cYyKColor.NoneK;xO}x}K

)U>,]`_a1Yenumsspecifies tag types with enumswrites enumsswitch, enumsStack with enumsunderlying typeGetUnderlyingTypecauses null errorNone enum