Tuesday 21 August 2018

C#: Enums

Enums are used to define named constants. 

First let me explain an example without enums and later we can see how can make the same application more readable using enums.

Program.cs
using System;

class Program
{
    /* Color_Code Color name *
     *      1       Green
     *      2       Red
     *      3       White
     *      4       Blue
     *      5       Yellow */
    public class Student
    {
        public String name;
        public int id;
        public int color;

        public Student(String name, int id, int color)
        {
            this.name = name;
            this.id = id;
            this.color = color;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public int getId()
        {
            return id;
        }

        public void setId(int id)
        {
            this.id = id;
        }

        public int getColor()
        {
            return color;
        }

        public void setColor(int color)
        {
            this.color = color;
        }

    }

    public static void printStudentFavoriteColor(Student stud)
    {
        Console.Write("{0} ", stud.getName());
        switch (stud.getColor())
        {
            
            case 1:
                Console.WriteLine("likes the Green color");
                break;
            case 2:
                Console.WriteLine("likes the Red color");
                break;
            case 3:
                Console.WriteLine("likes the White color");
                break;
            case 4:
                Console.WriteLine("likes the Blue color");
                break;
            case 5:
                Console.WriteLine("likes the Yellow color");
                break;
            default:
                Console.WriteLine("No specific color");
                break;
            
        }
    }

    static void Main(string[] args)
    {
        Student s1 = new Student("Hari Krishna", 1, 1);
        Student s2 = new Student("Gopi Battu", 2, 2);
        Student s3 = new Student("Sravya Guruju", 3, 3);
        Student s4 = new Student("Rachit Kumar", 4, 1);

        printStudentFavoriteColor(s1);
        printStudentFavoriteColor(s2);
        printStudentFavoriteColor(s3);
        printStudentFavoriteColor(s4);
    }
}

Output
Hari Krishna likes the Green color
Gopi Battu likes the Red color
Sravya Guruju likes the White color
Rachit Kumar likes the Green color

Notify following snippet, it displays specific message depends on the student color property. Since our application using an integer to represent student color, it is very difficult to remember the number that mapped to the color.

 switch (stud.getColor())
        {
            
            case 1:
                Console.WriteLine("likes the Green color");
                break;
            case 2:
                Console.WriteLine("likes the Red color");
                break;
            case 3:
                Console.WriteLine("likes the White color");
                break;
            case 4:
                Console.WriteLine("likes the Blue color");
                break;
            case 5:
                Console.WriteLine("likes the Yellow color");
                break;
            default:
                Console.WriteLine("No specific color");
                break;
            
        }


By using enums, we can make the same application more readable and maintainable.

How to define enum?
Enum keyword is used to define enums. For example following snippet defines the color constants.

   public enum Color
    {
        GREEN, RED, WHITE, BLUE, YELLOW, NO_FAVORITE_COLOR
    }

     public class Student
    {
        public String name;
        public int id;
        public Color  color;

  ...........
  ...........
 }
printStudentFavoriteColor method can be rewritten like below.

   public static void printStudentFavoriteColor(Student stud)
    {
        Console.Write("{0} ", stud.getName());
        switch (stud.getColor())
        {
            
            case Color.GREEN:
                Console.WriteLine("likes the Green color");
                break;
            case Color.RED:
                Console.WriteLine("likes the Red color");
                break;
            case Color.WHITE:
                Console.WriteLine("likes the White color");
                break;
            case Color.BLUE:
                Console.WriteLine("likes the Blue color");
                break;
            case Color.YELLOW:
                Console.WriteLine("likes the Yellow color");
                break;
            default:
                Console.WriteLine("No specific color");
                break;
            
        }
    }

Following is the complete working application.


Program.cs

using System;

class Program
{
    /* Color_Code Color name *
     *      1       Green
     *      2       Red
     *      3       White
     *      4       Blue
     *      5       Yellow */

    public enum Color
    {
        GREEN, RED, WHITE, BLUE, YELLOW, NO_FAVORITE_COLOR
    }

    public class Student
    {
        public String name;
        public int id;
        public Color  color;

        public Student(String name, int id, Color color)
        {
            this.name = name;
            this.id = id;
            this.color = color;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public int getId()
        {
            return id;
        }

        public void setId(int id)
        {
            this.id = id;
        }

        public Color getColor()
        {
            return color;
        }

        public void setColor(Color color)
        {
            this.color = color;
        }

    }

    public static void printStudentFavoriteColor(Student stud)
    {
        Console.Write("{0} ", stud.getName());
        switch (stud.getColor())
        {
            
            case Color.GREEN:
                Console.WriteLine("likes the Green color");
                break;
            case Color.RED:
                Console.WriteLine("likes the Red color");
                break;
            case Color.WHITE:
                Console.WriteLine("likes the White color");
                break;
            case Color.BLUE:
                Console.WriteLine("likes the Blue color");
                break;
            case Color.YELLOW:
                Console.WriteLine("likes the Yellow color");
                break;
            default:
                Console.WriteLine("No specific color");
                break;
            
        }
    }

    static void Main(string[] args)
    {
        Student s1 = new Student("Hari Krishna", 1, Color.GREEN);
        Student s2 = new Student("Gopi Battu", 2, Color.RED);
        Student s3 = new Student("Sravya Guruju", 3, Color.WHITE);
        Student s4 = new Student("Rachit Kumar", 4, Color.GREEN);

        printStudentFavoriteColor(s1);
        printStudentFavoriteColor(s2);
        printStudentFavoriteColor(s3);
        printStudentFavoriteColor(s4);
    }
}

Output
Hari Krishna likes the Green color
Gopi Battu likes the Red color
Sravya Guruju likes the White color
Rachit Kumar likes the Green color


Can I nested an enum inside a class?
Yes, you can define an enum inside a class.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.


Program.cs
using System;

class Program
{
    enum DaysOfWeek { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

    static void Main(string[] args)
    {
        Console.WriteLine("DaysOfWeek.Sat : {0}", (int)DaysOfWeek.Sat);
        Console.WriteLine("DaysOfWeek.Sun : {0}", (int)DaysOfWeek.Sun);
        Console.WriteLine("DaysOfWeek.Mon : {0}", (int)DaysOfWeek.Mon);
        Console.WriteLine("DaysOfWeek.Tue : {0}", (int)DaysOfWeek.Tue);
        Console.WriteLine("DaysOfWeek.Wed : {0}", (int)DaysOfWeek.Wed);
        Console.WriteLine("DaysOfWeek.Thu : {0}", (int)DaysOfWeek.Thu);
        Console.WriteLine("DaysOfWeek.Fri : {0}", (int)DaysOfWeek.Fri);
    }
}

Output

DaysOfWeek.Sat : 0
DaysOfWeek.Sun : 1
DaysOfWeek.Mon : 2
DaysOfWeek.Tue : 3
DaysOfWeek.Wed : 4
DaysOfWeek.Thu : 5
DaysOfWeek.Fri : 6

Notify above snippet, first enum consant ‘Sat’ starts from value 0, and each subsequent enums are incremented by 1. You can override this default value.


Program.cs

using System;

class Program
{
    enum DaysOfWeek { Sat=6, Sun, Mon, Tue, Wed, Thu, Fri };

    static void Main(string[] args)
    {
        Console.WriteLine("DaysOfWeek.Sat : {0}", (int)DaysOfWeek.Sat);
        Console.WriteLine("DaysOfWeek.Sun : {0}", (int)DaysOfWeek.Sun);
        Console.WriteLine("DaysOfWeek.Mon : {0}", (int)DaysOfWeek.Mon);
        Console.WriteLine("DaysOfWeek.Tue : {0}", (int)DaysOfWeek.Tue);
        Console.WriteLine("DaysOfWeek.Wed : {0}", (int)DaysOfWeek.Wed);
        Console.WriteLine("DaysOfWeek.Thu : {0}", (int)DaysOfWeek.Thu);
        Console.WriteLine("DaysOfWeek.Fri : {0}", (int)DaysOfWeek.Fri);
    }
}

Output

DaysOfWeek.Sat : 6
DaysOfWeek.Sun : 7
DaysOfWeek.Mon : 8
DaysOfWeek.Tue : 9
DaysOfWeek.Wed : 10
DaysOfWeek.Thu : 11
DaysOfWeek.Fri : 12

You can also give different value for every enum constant.


Program.cs

using System;

class Program
{
    enum DaysOfWeek { Sat = 6, Sun = 7, Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5 };

    static void Main(string[] args)
    {
        Console.WriteLine("DaysOfWeek.Sat : {0}", (int)DaysOfWeek.Sat);
        Console.WriteLine("DaysOfWeek.Sun : {0}", (int)DaysOfWeek.Sun);
        Console.WriteLine("DaysOfWeek.Mon : {0}", (int)DaysOfWeek.Mon);
        Console.WriteLine("DaysOfWeek.Tue : {0}", (int)DaysOfWeek.Tue);
        Console.WriteLine("DaysOfWeek.Wed : {0}", (int)DaysOfWeek.Wed);
        Console.WriteLine("DaysOfWeek.Thu : {0}", (int)DaysOfWeek.Thu);
        Console.WriteLine("DaysOfWeek.Fri : {0}", (int)DaysOfWeek.Fri);
    }
}


Output

DaysOfWeek.Sat : 6
DaysOfWeek.Sun : 7
DaysOfWeek.Mon : 1
DaysOfWeek.Tue : 2
DaysOfWeek.Wed : 3
DaysOfWeek.Thu : 4
DaysOfWeek.Fri : 5

By default enum takes an integer as underlying type to every enum constant. You can also specify any other integral type for this.


enum DaysOfWeek : short { Sat = 6, Sun = 7, Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5 };


Previous                                                 Next                                                 Home

No comments:

Post a Comment