How To Create And Use C# Enums

ENUM in C# .NET

In this article, I will explain about enums in C# and how to use them in your app. 

enum

What is Enum?

Enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user-defined.

Enums type can be an integer (float, int, byte, double etc.) but if you use beside int, it has to be cast.

Enum is used to create numeric constants in .NET framework. All member of the enum are of enum type. There must be a numeric value for each enum type.

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

Program to demonstrate how to create and use an Enum

  1. using System;  
  2. namespace example_enum {  
  3.     class Program {  
  4.         public enum DayofWeek {  
  5.             Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday  
  6.         }  
  7.         static void Main(string[] args) {  
  8.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Sunday, DayofWeek.Sunday);  
  9.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Monday, DayofWeek.Monday);  
  10.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Tuesday, DayofWeek.Tuesday);  
  11.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Wednesday, DayofWeek.Wednesday);  
  12.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Thursday, DayofWeek.Thursday);  
  13.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Friday, DayofWeek.Friday);  
  14.             Console.WriteLine("Day of week {0} {1}", (int) DayofWeek.Saturday, DayofWeek.Saturday);  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
  18. }  

Some points about enum

  • Enums are enumerated data type in C#.
  • Enums are not for end-users, they are meant for developers.
  • Enums are strongly typed constants. They are strongly typed, i.e., an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • enum values are fixed. enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.

Can you give two same values in enum type?

Yes, we can have the same value in the enum type. For example - when we want to set priority options like,

  • Normal 0
  • Excellent 1
  • Default 0
  • Good 3

Program showing enum type having same values

  1. using System;  
  2. namespace enum_example4 {  
  3.     class Program {  
  4.         public enum DayofWeek {  
  5.             Sunday = 1, Monday, Tuesday = 1, Wednesday, Thursday = 2, Friday, Saturday  
  6.         }  
  7.         static void Main(string[] args) {  
  8.             string[] values = Enum.GetNames(typeof(DayofWeek));  
  9.             foreach(string s in values) {  
  10.                 Console.WriteLine(s);  
  11.             }  
  12.             Console.WriteLine();  
  13.             int[] n = (int[]) Enum.GetValues(typeof(DayofWeek));  
  14.             foreach(int x in n) {  
  15.                 Console.WriteLine(x);  
  16.             }  
  17.             Console.ReadLine();  
  18.         }  
  19.     }  
  20. }  

Program to find out the number of values in enum

  1. using System;  
  2. namespace enum_exampl3 {  
  3.     class Program {  
  4.         public enum DayofWeek {  
  5.             Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday  
  6.         }  
  7.         static void Main(string[] args) {  
  8.             string[] values = Enum.GetNames(typeof(DayofWeek));  
  9.             int total = 0;  
  10.             foreach(string s in values) {  
  11.                 Console.WriteLine(s);  
  12.                 total++;  
  13.             }  
  14.             Console.WriteLine("Total values in enum type is : {0}", total);  
  15.             Console.WriteLine();  
  16.             int[] n = (int[]) Enum.GetValues(typeof(DayofWeek));  
  17.             foreach(int x in n) {  
  18.                 Console.WriteLine(x);  
  19.             }  
  20.             Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  

Above code review

In the above program, you have string and numeric representation of enum values. Enum class contains many useful methods for working with enumerations. The beauty of enum is that your can process it as integer value and display as string.

Hope the article would help you in understanding enum and their usage in C# and .NET. Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now