HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Enums in C#
An enum type is a distinct value type. A simple example of enum is shown below.
  • In our example we have a Color enum. The different colors in the enum are Red, Green and Blue. By default enums will have underlying integer values starting at 0. So in our example Red, Green and Blue will have integer values as listed blow.
    1. Red - 0
    2. Green - 1
    3. Blue - 2
    Note: So the default underlying type for an enum is integer and the values start from 0.
  • In the Main method we prompt the user to choose a color - 0 for Red : 1 for Green : 2 for blue. Once the user enters his input, we convert the input to an integer and subsequently to the enum type.
  • We then use the switch conditonal statement to determine what color the user has chosen. For a clear understainding of switch statement and all other conditional statements please read conditional statements in C# article.
  • Inside the switch statement we make use of the Color enum we have defined rather than using 0,1 and 2. Using enums presents usability. Dotnet framework also has made extensive use of enums. For example in ASP.NET we have an enmu TextBoxMode which takes either SingleLine, MultiLine or Password.

using System;

public enum Color

{

    Red,

    Green,

    Blue

}

             

public class MainClass

{

    public static void Main()

    {

        Console.WriteLine("Which color do you want? 0 for Red : 1 for Green :         

                           2 for blue");

        Color color = (Color)Convert.ToInt32(Console.ReadLine());

             

        switch (color)

        {

           

            case Color.Red:

                Console.WriteLine("You want Red");

                break;

            case Color.Green:

                Console.WriteLine("You want Green");

                break;

            case Color.Blue:

                Console.WriteLine("You want Blue");

                break;

            default:

                Console.WriteLine("You did not select Red,Green or Blue");

                break;

        }

    }

}

Points to remember about enums
  • From the simple enum example above we understood that, the default underlying type for an enum is integer and the values start from 0. We can change this behaviour by specifying the type next to the name of the enum and from what values do we want to have inside enum.
  • The below code snippet sets the underlying type of enum to byte and Red,Green and Blue will have values of 10,20 and 30 respectively.
  • The underlying type of an enum can be any one of the following types only. It is a compile time error to specify any other type.
    1. byte
    2. sbyte
    3. short
    4. ushort
    5. int
    6. uint
    7. long
    8. ulong
  • In the Main method
      byte b = Color.Red;
    The above line would generate a compile time error. Though the underlying type for enum is byte, dotnet will not implicitly convert the enum to its underlying type. We have to make use of explicit conversion as shown in the example. This is why enums are also called as type safe constants.
  • Also remember 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.
  • Enums are value types. For a clear understanding of Value types, reference types and differences please read Types in C# article.

using System;

public enum Color: byte

{

    Red   = 10,

    Green = 20,

    Blue  = 30

}

       

public class MainClass

{

    public static void Main()

    {

       

        //byte b = Color.Red; //Compile time error

        //Cannot implicitly convert type 'Color' to 'byte'

       

        byte b = (byte)Color.Red; // Correct explicit conversion required

    }

}

Njoy Programming
   ByPrasad Cherukuri