Through this article we will talk about what are enum (enumeration) , how and where we can use it. The enum is a keyword which we use when we want to associate some constant values with a name. Enumeration are strongly typed and appear inside IntelliSense and therefore save a lot of time 🙂  By default enumeration are zero based so first value is equal to 0 and each following is incremented by one. You can change starting and all other values to your liking, but remember that all subsequent values will also be incremented starting of last custom value. Enough of theory, lets jump to examples.

Example 1

Lets say we want to print to Console numerical day of the week.

There is nothing wrong with this example. But, what if workweek starts on some other day? You will have to change all values. How can we avoid this happening to us? We can use enums!

We declared enum with enum keyword, then we gave it a meaningful name, then inside curly brackets we wrote days of the week. To display numeric value I have explicitly changed value to int with (int). Also order of days is slightly change and Sunday is now on the first place so you should get changed output when you run the program.

Example 2

What if I want to give custom value to my enums? It’s easily accomplished. We will us our previous example.

As you can see Sunday has a value 1 and next three days have incremented by 1, but when we got to Thursday its value changed to 8, Friday got incremented to 9.  In Saturday case we set its value to Thursday + Friday and got output of 17 just to show you that option as well.

 

That covers enums. Stay tuned for more!