How to Use Enums in C#

Introduction

Enum is a strongly typed constant and keyword defined for the Enumeration new data type. A Typesafe enumeration provides an efficient way to define a set of named integral constants that may be assigned to a variable. Using enums makes the code more readable and less prone to errors. Enums are useful to developers when you have a set of values that are functionally significant and unchanged. The main advantage of Enums is to make it easy to change values in the future. Enums are a robust alternative to the simple String or Int constants used in much older APIs to represent sets of related items.

Using Enums in C#

Enums written by developers are used to create numeric constants in the .NET framework. All members of the enum must be assigned a numeric value. Following are the key points about Enums:

  • Keyword Enums create enumerated data types in C#
  • Enums are for developers
  • As mentioned above, Enums are strongly typed constants
  • An enum of one type can’t be implicitly assigned to another enum type
  • Enumerations make code much more readable, reusable, and understandable
  • Enum values are fixed
  • The default enum type is int
  • Every enum type automatically derives from System.Enum
  • Enums are value types and are created on the stack, not on the heap

C# Enum is an abstract class; it has static helper methods to work with. Following is the list of helper methods:

  • Format: This function converts the specified value of enum type to the specified string format
  • GetName: This function returns the name of the constant or value of the specified enum type
  • GetNames: It returns an array of string name of all the constant of the specified enum
  • GetValues: An array of the values of all the constants of the specified enum
  • Parse: The function converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object
  • TryParse: Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object

In the following C# program, I have created two Enum types, named DefectPrioriy and DefectSeverity. Both DefectPrioriy and DefectSeverity enumerations have four values. In the main program, I have printed values and names of both the enumerations using the console.write and console.writeline functions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumProject
{
   enum DefectPrioriy
   {
      Immediate = 1,
      High,
      Medium,
      Low
   }
   enum DefectSeverity
   {
      Critical = 1,
      Major,
      Minor,
      Low
   }
   class Program
   {
      static void Main(string[] args)
      {
         Console.Write("Defect Prioriy: ");
         Console.WriteLine((int)DefectPrioriy.Immediate);
         Console.Write("Defect Severity: ");
         Console.WriteLine((int)DefectSeverity.Minor);

         string sDefectPrioriy = Enum.GetName(typeof
            (DefectPrioriy), 4);
         Console.WriteLine(sDefectPrioriy);

         Console.WriteLine("The values of the DefectPrioriy Enum
            are:");
         foreach (int i in Enum.GetValues(typeof(DefectPrioriy)))
            Console.WriteLine(i);

         Console.WriteLine("The names of the DefectPrioriy Enum
            are:");
         foreach (string str in Enum.GetNames(typeof
               (DefectPrioriy)))
            Console.WriteLine(str);
            PrintEnumWithoutLoop();
            DisplayEnumSwitchCase();
            Console.Read();

      }
   }
}

The following two functions, PrintEnumWithoutLoop and DisplayEnumSwitchCase, are called from the main method to display a defect priority inside a switch case and without a loop.

static void PrintEnumWithoutLoop ()

{
   Console.WriteLine("Defect Prioriy {0} {1}",
      (int)DefectPrioriy.Immediate, DefectPrioriy.Immediate);
   Console.WriteLine("Defect Prioriy {0} {1}",
      (int)DefectPrioriy.High, DefectPrioriy.High);
   Console.WriteLine("Defect Prioriy {0} {1}",
      (int)DefectPrioriy.Medium, DefectPrioriy.Medium);
   Console.WriteLine("Defect Prioriy {0} {1}",
      (int)DefectPrioriy.Low, DefectPrioriy.Low);
   Console.ReadLine();
}
static
void DisplayEnumSwitchCase()
{
   DefectPrioriy value = DefectPrioriy.Medium;
   switch (value)
   {
      case DefectPrioriy.Immediate:
         Console.WriteLine("Defect Priority Immediate");
         break;
      case DefectPrioriy.High:
         Console.WriteLine("Defect Priority High");
         break;
      case DefectPrioriy.Medium:
         Console.WriteLine("Defect Priority Medium");
         break;
      case DefectPrioriy.Low:
         Console.WriteLine("Defect Priority Low");
         break;
   }
}

Conclusion

I hope this article has helped you understand enums, and their usage in C# and .NET applications. Please provide your valuable feedback for improvement. That’s all for today; happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read