Enums in C# with Example

Enums in C# with Example
Rating: 5 - 1 Votes

Enums in C# with Example

C#Enums stands for enumerations.  It is basically a set of integral numbers.  Enums are value types.  Enums make your program more readable as well as more maintainable.

I know since you are new to C#, the above lines do not make sense to you at all.  Lets understand enums with a very basic example.  Whenever a developer creates a program, he distributes it with certain restrictions.  There are 3 modes in that program (Trial, Active, & Expired).  Trial users can use the program for a fixed number of days.  Active users can use the program without any restrictions.  Expired users can no longer user the program since their subscription is expired.

Now, we can create a custom function to know about the user’s subscription and run the program in that mode.   We can return integer values after finishing our checks like this:

  • Trial -> 0
  • Active -> 1
  • Expired -> 2

So, based on these integer values, we will decide in which mode the program should run.  I know this sounds easy here, but there is a catch.  We can add more modes to it like Professional Edition, Developer Edition, Standard Edition etc.  Do you think, you or your team members can remember integral values of numerous modes during development?  Of course, not!

In such situation, enums make the work easy for you and your team members.  I know you are thinking why not we just return a string and compare it with another?  I hope you know that a slight change in case of a character or any kind of additional space in the string can give wrong result.  To create an enum, we make use of enum keyword.  Example is given below:

Enums are strongly typed constants and the default underlying data type is integer.  You cannot assign an integer value directly to an enum without type casting and vice versa.  If you have 2 enums, then an enum of one type cannot to be assigned to enum of another type directly even though the underlying values of members are same.  This is only possible with explicit type cast.  Example is given below.

To get default integral values, we make use of GetValues() static method which resides in Enum class.  Due to strongly typed nature, an explicit cast of int is required.  Similarly, to get names, we make use of GetNames(), but an explicit cast of string is not required in this case.  The point to remember here is, enum in small case is a keyword to create enums.  Enum with E in upper case is for Enum class.  Example is given below

You can also change default integer values of elements in enums and can assign custom values to each of them.  The integral values of elements get incremented by 1 automatically.  This means if value of first element is 10, then next element value will be 11.  However, by default, first element value is always 0.  Example is given below:

You can also change default underlying data type of enums from int to any other data type like short, long etc.  Example is given below: