Site icon FahmidasClassroom

Use of Enumeration(Enum) in C#

Feature Image

The enumeration or enum is one type of user-defined data types that is defined by the coder. The set of related constant values is declared by the enumeration. It makes the code easier to understand. The way of declaring and using enum data type in C# has been shown in this tutorial.

Syntax:

enum <Name>
{
    <value1>,
    <value2>,
    <value3>,
    ...
    <valueN>
}

or

enum <Name> : <dataType>
{
    <value1> = <setVal1>,
    <value2> = <setVal2>,
    <value3> = <setVal3>,
    ...
    <valueN> = <setValN>
}

Example-1: Simple use of Enumeration

Create C# file with the following code to know the use of simple enum type in the C# code. Here , enum type named Weekday has been declared with seven data members. Next, an input is taken from the user and check with the particular enum value and print the corresponding message.

using System;
namespace ConsoleApp1
{
    //Declare a simple enum data type
    enum Weekday
    {
        //List of data members
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday
    }

    class Program
    {

        //Define the main() Method
        static void Main(string[] args)
        {
            //Print the message to take weekday number from the user
            Console.WriteLine("Enter the weekday number:");
            //Convert input value into the integer
            int wday = Convert.ToInt16(Console.ReadLine());
            //Check the input value
            if (wday == (int) Weekday.Friday)
            {
                Console.WriteLine("Today is holiday.");
            }
            else
            {
                Console.WriteLine("Go to work...");
            }
        }
    }

}

Run the code and take 3 as input value. According to the enum value, 3 matches with the value, Tuesday. So, the message, “Go to work…” will be printed.

Run the code again and take 5 as input value. According to the enum value, 5 matches with the value, Friday. So, the message, “Today is holiday” will be printed.

Example-2: Use of enum with type and values

Create C# file with the following code to know the use of enum type with values in the C# code. Here , enum type named Month of four data members with values has neen declared. Next, an input is taken from the user and check with the particular enum value and print the corresponding message.

using System;


namespace ConsoleApp1
{
    //Declare the enum type with type and values
    enum Month : byte
    {
        June = 6,
        July = 7,
        December = 12,
        January = 1
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Print the message to take month in number from the user
            Console.WriteLine("Enter the month in number:");
            //Convert input value into the byte
            byte mm = Convert.ToByte(Console.ReadLine());
            //Check the input value
            if ((byte)Month.June == mm || (byte)Month.July == mm)
            {
                Console.WriteLine("Summer vacation (" + $"{Month.June}" + " - " + $"{Month.July})" +".");
            }
            else if ((byte)Month.December == mm || (byte)Month.January == mm)
            {
                Console.WriteLine("Winter vacation (" + $"{Month.December}" + " - " + $"{Month.January})" +".");
            }
            else
            {
                Console.WriteLine("No vacation.");
            }
        }
    }
}

Run the code and take 6 as input value. According to the enum value, 6 matches with the value, June. So, the message, “Summer vacation (June – July)” will be printed.

Run the code and take 12 as input value. According to the enum value, 12 matches with the value, December. So, the message, “Winter vacation (December – January)” will be printed.

Solve the following exercise to practice the use of the enumeration in C#.NET.

Exercise:

Write a C# code to implement the traffic light system by using enumeration where the data member of the enum type will be Red, Yellow, and Green. Print the message based on the following criteria.

Exit mobile version