Enumerations in C# – Free C# Tutorials

In this blog we will discuss about Enumerations in C#

  • An enumeration is a user-defined integer type which provides a way of attaching names to  numbers.
  • The enum keyword automatically enumerates a list of words assigning them values 0.1.2 and so on.
  • This facility provides an alternative means of creating ‘constant’ variable names.

The syntax of enum statement is illustrated below:

enum Shape
{
 Circle;      //ends with a comma
 Square;   //ends with comma
 Triangle;  //no comma
}

The above code can also be written as :

enum Shape { Circle , Square , Triangle }

  • Here, Circle tales the value 0,Square takes the value 1 and Triangle takes the value 2.Some other examples of enum are listed below:

enum OperatingSystem { MacOs , Windows ,Linux }

enum Day { Mon , Tue , Wed , Thur , Fri , Sat , Sun }

code:

using System;
Class Area
{
  public enum Shape
  {
    Circle,
    Square
  }
  public void AreaShape ( int x , Shape shape )
  {
    double area;
    switch(shape)
    {
      case    Shape.Circle;
      area = MATH.PI*x*x;
      Console.WriteLine("circle area =" +area );
      break;
      case   Shape.Square
      area = x * x;
      Console.WriteLine("square area =" +area );
      break;
      default:
      Console.WriteLine(" Invalid input" );
      break;
     }
   }
}
class EnumTest
{
  public static void Main()
  {
    Area area = new Area();
    area.AreaShape(15 , Area.Shape.Circle);
    area.AreaShape(15 , Area.Shape.Square);
    area.AreaShape(15 , (Area.Shape)  1);
    area.AreaShape(15 , (Area.Shape)  10);
  }
}

Explanation

  • The shape enum defines the values that can be specified foe the enum, and the same enum is used in the method call to specify the shape whose area we want
  • This is how the the enum members are accessed

shape.Circle  // access to circle inside the Area class

Area.Shape.Circle  // access to circle outside Area class

  • In the method calls, the values  that can be specified for an enum are not limited to the identifiers specified in the enum definition.The third and fourth calls:

area.AreaShape(15 , (Area.Shape)  1);

area.AreaShape(15 , (Area.Shape)  10);

are legal calls.However , the values passed must be in the range of valid values.Since the value of enum members range from 0 to 1, the third call refers to the case Shape.Square while fourth call passes a value that is outside  the range.

Note: when int type values are passed, they are cast into enum type values.

The output for the above code

Circle Area = 706.8583
Square Area = 225.0000
Square Area = 225.0000
Invalid data


Enumerator initialization

As mentioned above the default the first enum member is set to 0,and that of each next member is incremented by 1.However we can assign specific values to the enum values as follows:

Example:

enum Cars
{
   Tesla = 1,
   swift = 3,
   Hyundai = 2
   BMW = 4
}

we can also have expressions, as long as they use the already defined enum numbers.

Example:

enum Cars
{
  Tesla = 1,
  swift = Tesla + 2,
  Hyundai = Tesla + swift + 3
}

If the declaration of the enum member has no initializer,then it’s value is set implicitly as follows:

  1. If it is the first member,it’s value is zero.
  2. Otherwise, it’s value is obtained by adding one to the value of the previous member.

Enumerator Base Types:

By default,the type of an enum is int.But we can explicitly declare a base type for each enum. The valid base types:

  • byte , sbyte , short , ushort , int , uint , long and ulong

Example:

enum Position : byte
{
   Off,
   On
} 
enum Shape : long
{
   Circle,
   Square = 100,
   Traingle
}

The values assigned to the members must be within the range of values that can be represented by the base type.For Example, if the base type is byte, assigning a value 300 is illegal.

Leave a Comment

Your email address will not be published. Required fields are marked *