How to use Implementing Enumerations type in C#


Introduction:
In this articles we will see Implementing Enumeration type in C#.Enumeration is a user defined integer type which provides a way for attachement names to numbers thereby increasing the comprehensibillity of the code.the enum keywords automatically a list of words by assigning them values 0,1,2 and so on.this facility provides an alternative means of creating 'constant' variable names.the syntax of an enum statement is illustrated below:
enum shape
{
Circle, //ends with comma
square//no comma
}

Example:

 using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
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("CircleArea="+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);
        }
    }
 
Output of th eprogram:

enum.gif

Thank U. if U have any Suggestions and Comments about my blog, Plz let me know.

Ebook Download
View all
Learn
View all