Home Basics C# Enum

C# Enum

An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. To define an enumeration type, use the enum keyword and specify the names of enum members:

An enum in C# is declared by the enum keyword followed by the enumeration name and its values within a bracket.

By default, the associated constant values of enum members are of type int; they start with zero and increase by one following the definition text order.

In c#, an enumeration can contain only integral data types such as byte, sbyte, short, ushort, int, uint, long or ulong.

Lets see some examples of enum

enum Season { Spring, Summer, Autumn, Winter }
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };  
enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };    
enum Colors { Yellow, Orage, Red, Green, Blue, Purple, Black };

You can change the default values of an enumerator by assigning a new value to the first item in the enumerator which will automatically assign incremental values to the successive items in an enumerator.

 

Lets see an example

enum Season 
{ 
Spring = 10, 
Summer, 
Autumn, 
Winter 
}

If you wanted to change the default integral type of elements in an enumeration from an int to byte, you can do something like this.

enum Season: byte 
{ 
Spring, 
Summer, 
Autumn, 
Winter 
}

Lets see an example

Example 1

Pretty basic example

using System;

public class Program
{
	enum Season
	{ 
		Spring, 
		Summer, 
		Autumn, 
		Winter 
	}
	
	public static void Main()
	{
		int a = (int)Season.Spring;
		int b = (int)Season.Autumn;
		Console.WriteLine(Season.Spring);
		Console.WriteLine(Season.Autumn);
		Console.WriteLine("Spring: {0}", a);
		Console.WriteLine("Autumn: {0}", b);
		Console.WriteLine("\nPress Enter Key to Exit..");
		Console.ReadLine();
	}
}

Run this and you should see the following

Spring
Autumn
Spring: 0
Autumn: 2

Press Enter Key to Exit..

Looping an enum

The GetValues method of System.Enum can be used to loop through an enum values and the GetNames method of System.Enum returns a string value of each elements of an enum.

using System;

public class Program
{
	enum Season
	{ 
		Spring, 
		Summer, 
		Autumn, 
		Winter 
	}
	
	public static void Main()
	{

		Console.WriteLine("Read values of the Season enum");  
   	    foreach (int i in Enum.GetValues(typeof(Season)))  
        	Console.WriteLine(i);
		
    	Console.WriteLine("Read names of the Season enum"); 
    	foreach (string str in Enum.GetNames(typeof(Season)))  
       		 Console.WriteLine(str);  

		Console.WriteLine("\nPress Enter Key to Exit..");
		Console.ReadLine();
	}
}

Run this and you should see as follows

Read values of the Season enum
0
1
2
3
Read names of the Season enum
Spring
Summer
Autumn
Winter

Press Enter Key to Exit..

enum to a list

You can convert the values of an enum  into a List object.

Here is an example of this

using System;
using System.Collections.Generic;

public class Program
{
	enum Season
	{ 
		Spring, 
		Summer, 
		Autumn, 
		Winter 
	}
	
	public static void Main()
	{
		List<string> seasonsList = new List<string>(Enum.GetNames(typeof(Season)));  
		foreach (string season in seasonsList)  
    		Console.WriteLine(season);  

		Console.ReadLine();
	}
}

enum to array

You can converts enum values into an array. Here is an example.

using System;
using System.Collections.Generic;

public class Program
{
	enum Season
	{ 
		Spring, 
		Summer, 
		Autumn, 
		Winter 
	}
	
	public static void Main()
	{		
		string[] seasons = Enum.GetNames(typeof(Season));  
		foreach (string season in seasons)  
   			 Console.WriteLine(season); 
		Console.ReadLine();
	}
}

C# Enum Methods

In c#, we have a class called Enum that contains the following methods to work with an enumeration.

Method Description
Format convert the value of enum type to a specified string format.
GetName get the name of the specified enum item.
GetNames  get all item names of the specified enum as an array.
GetValues get all item values of the specified enum as an array.
Parse convert the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
GetUnderlyingType return the underlying type of the specified enumeration.

You may also like