Enumeration use for to assign multiple constant integral values to a single variable. Enumeration improves code clarity and makes program easier to maintain easy way. Enumeration in C# also provides more security by better error-checking technology and compiler warnings.

An enumeration can be defined using enum keyword. In enumeration, you can define special set of value that can be assigned with enumeration. For Example, you are creating an attendance log application in which a variable can contains value only Monday to Friday. The other value will not be applicable with variables. In order to fulfill this requirement you need to use enumeration that will hold only assigned values and will returns numeric position of values starting with zero.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Enumeration
{
    // creating enumeration for storing day.
    public enum Attandance
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday
    }
    class Program
    {
        static void Main(string[] args)
        {
            Attandance present = Attandance.Monday;//Valid
            Console.WriteLine(present);
            //Attandance absent = Attandance.Sunday;//Invalid
            Console.ReadLine();
        }
    }
}

Leave a Reply

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