Enumerations in C#

Enumerations in C# are used to hold collection of values where each value corresponds to an integer.

MSDN defines enum as: An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

This might seem complex at the beginning but the concept is quite simple. Consider a scenario where you want to store months in a year and you want to make sure that when you write January, you get integer 1; similarly for February you get 2. In such cases, enumerations can be very handy. Have a look at the following example.

using System;

namespace CSharpTutorials {
    enum months { Jan, Feb, Mar, Apr, May, Jun, July, Aug, Sep, Oct, Nov, Dec };
    class Tutorial {
		static void Main(string[] args) {
			months monthofyear;
			monthofyear = months.Apr;

			int firstmonth = (int)months.Jan;
			int lastmonth = (int)months.Dec;

			Console.WriteLine("January is the: {0} month.", firstmonth+1);
			Console.WriteLine("December is the: {0} month.", lastmonth+1);
			Console.ReadKey();
		}
	}    
}

Download the code
Enumerations are created using the keyword enum followed by the name of the enumeration and curly brackets, respectively. Inside the curly brackets, the items of the enumeration are listed in the form of string literal. Each item is separated by a comma. In the above code, an enumeration named months has been defined which contains months within a year.

Enumeration variable can only contain one of the values of the enumeration. In the above code monthofyear variable of enumeration of type months have been defined and the month of Apr has been assigned to it.

Like arrays, enumerations follow zero based index which means that the integer value for first item is zero and for last item is k-1 where k is the size of the enumeration. Therefore, you need to add one to the integer value of enumeration item, in order to get actual associated integer value of the enumeration.

Few other facts about C# enums:

  • Enums can’t inherit from other enums. All enums are inherited from a built in System.Enum
  • To get the int value of an enum, simply cast in to int using (int) prefix. Here is an example.
  • You can use: Enum.GetNames (typeof (MyEnum)) to get an array of all possible names inside the enum. Then you can use .Length property of the resulting array to get the size of the enum.
  • You can use: Enum.GetValues (typeof (MyEnum)) to get an array of all possible values inside the enum.
  • Enum declaration can optionally be decorated with [Flags]. This will enable the enum to represent a collection of flags as opposed to a single value. These collections then can be manipulated using bitwise operators. Also once you decorate the definition of an enum using [Flags], you can use ToString() function to extract the name of the enum in string format. This is very handy in situations where string representation of the enum and manipulation is needed. Here is some nice discussion about [Flags] on stack overflow.
<<< C# TutorialNullable Types in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .