Hello friends, I hope you all are having fun. In today's tutorial, we will have a look at detailed Introduction to Enums in C#. It's our 23rd tutorial in C# series and this C# concept is quite easy one. It's normally used to give better meanings/understanding to your project.If you haven't studied lectures on C# Classes & C# Methods, then do read them first, as we are going to use them today. So, let's get started with Introduction to Enums in C#:
Introduction to Enums in C#
- Enums in C# ( short for Enumerations ) is a value type datatype for constants, created by using keyword enum and can be controlled by the class Enum. ( keyword with small e, while class with capital E )
- We can declare & implement Enums directly in C# Namespace, class or struct and the underlying type of an enum is integer.
- Enums are strongly typed so we can't implicitly convert them into any other Data Type. Although we can use explicit conversion to convert enums into integers etc. ( We studied it in Data Types Conversions and we will also look at it shortly )
- Let's understand this enum concept with an example, suppose we are working on some scheduling project and we need to print names of all days.
- As we know these names are constants, and are also occurring in a sequence, so we can create enums for them, as shown in figure on right side.
- There are 3 ways to create Enums as you can see in the figure.
- In the first enum declaration, I have simply added the data in the enum WeekDays. Although, I haven't added any index but still it got the index starting from 0 and then incremented automatically by 1, thorough out the enum, as its underlying type is integer.
- In the second enum declaration, I have added the index in front of the data, so we can do that as well. Although, there's no difference between first & second implementation, its just second one displays the hidden values.
- In the third enum implementation, I have assigned random values to constants, so we can do that as well. Instead of values in a sequence, we can assign random values to these Constants.
Why use Enums in C# ?
- You must be thinking, why we need to use Enums, we can remember such things and can easily replace them with integer checks.
- Yeah we can do that in simple project but in complex projects and especially in those projects where you don't have a sequence, enums play an important role.
- Let's say you need to use Elements of Periodic Table in your project, there are too many elements, so in such cases, enum comes quite handy, you can place these elements in right order in your enum and then can use them easily though out your project.
How to use Enums in C# ???
- Now let's have a look at How to use Enums in C#, so I am creating a simple code where we will check whether it's holiday or working day using enums.
- As shown in below figure, I have created an Enum named WeekDays and this enum is public so we can use it in other classes.
- Moreover, enums are value type so, we can't instantiate them in other classes.
- You can see in above figure that in Main Function, I have created a new variable CurrentDay of Data Type WeekDays, as enum is a Data Type.
- We can't assign any other value to this variable, we can only assign members of its enum to this variable.
- In the IF Loop, I have used the members of enum in my conditions to verify.
- So, now you can see the code has become more Readable & we can easily maintain it without remembering anything.
- We have studied enum keyword so far, now let's have a look at class Enum:
How to use class Enum in C# ??
- In Visual studio, you will also find Enum class along with enum keyword. (enum keyword starts with small e while class Enum starts with capital E )
- We can use this Enum class to get different properties of our enums i.e. we can get their Constants or can also get their underlying integer values.
- We can also search for any keyword in the Enum, whether it exists or not.
- I have performed all these three operations in the code, shown in below figure:
- So, you can see in above figure that we can get Names & Indexes of our enums using this Enum class.
- I have first used the class name Enum, then dot operator and finally methods from Enum Class.
- We can also change the underlying type of our enum using ( : ) colon operator.
- In the right figure,I have changed the underlying type of my enum from integer to short.