Enumeration is basic C# type mostly used for increase readability of code and make variable type safety.
Assume that we would like to use weekdays in some variable. We can create create int variable for it and assume that following numers (1-7) are associated with following weekdays. This would be enough but every user which will read our code should know about our assumptions to have no problems with analyze. Additionally someone can accidentially assign value being out of range 1-7 which can cause unexpected problems.
Better solution for above problem is using enumeration. Let's see following example:
Output of this example is:
In point I we are declaring our weekdays enumeration. Declaring is vary easy - we are using enum NameOfEnumeration syntax putting enumeration values into curly brackets, separated by comas.
Point III shows how to use enumeration. We are using enumeration type as any other type in C#. We are creating variable of our enumeration type. After that we can assign only values from our enumeration into that variable. That mechanism protects us by problem of assigning out-of-range unexpected value to variable. When we are trying assign other value than enumeration value we will receive compilation error.
You can also notice that our code has better readability right now. Comparison of
is definitely better understandable than
In point II we are seeing one more use of enumeration types. We can create bit fields using enumerations which has better readability than just naming bits with following numbers. In order to declare it we are using [Flags] directive before enumeration declaration. As you can see we are defining following powers of 2 values to following bits representing LEDs.
In point IV we are setting bits our bit field using bit operators | (or) and we are checking whether bit is set using & (and) bitwise operator.
Above example you can find and compile github repository: https://github.com/xmementoit/CSharpAdventureExamples/tree/master/csharpBasics/enumsAndBitFields
Read more »
Assume that we would like to use weekdays in some variable. We can create create int variable for it and assume that following numers (1-7) are associated with following weekdays. This would be enough but every user which will read our code should know about our assumptions to have no problems with analyze. Additionally someone can accidentially assign value being out of range 1-7 which can cause unexpected problems.
Better solution for above problem is using enumeration. Let's see following example:
Output of this example is:
In point I we are declaring our weekdays enumeration. Declaring is vary easy - we are using enum NameOfEnumeration syntax putting enumeration values into curly brackets, separated by comas.
Point III shows how to use enumeration. We are using enumeration type as any other type in C#. We are creating variable of our enumeration type. After that we can assign only values from our enumeration into that variable. That mechanism protects us by problem of assigning out-of-range unexpected value to variable. When we are trying assign other value than enumeration value we will receive compilation error.
You can also notice that our code has better readability right now. Comparison of
day == Days.Sunday
is definitely better understandable than
day == 7
In point II we are seeing one more use of enumeration types. We can create bit fields using enumerations which has better readability than just naming bits with following numbers. In order to declare it we are using [Flags] directive before enumeration declaration. As you can see we are defining following powers of 2 values to following bits representing LEDs.
In point IV we are setting bits our bit field using bit operators | (or) and we are checking whether bit is set using & (and) bitwise operator.
Above example you can find and compile github repository: https://github.com/xmementoit/CSharpAdventureExamples/tree/master/csharpBasics/enumsAndBitFields