Friday, April 30, 2021

C# Enumeration

Enumeration is a data type which contains an sequence of names written are inside the body of the enum separated by comma. The names in the sequence have integral values. if the values are not assigned to the names of the enum datatype then the first name has value =0 and second name in sequence has value=1 and so on. But we can assign any arbitrary numeric values to the names as well. For example, enum Month {jan, feb, march, april} 
or, enum Month : int{jan=1, feb=2, march=3, april=4} 
Syntax
enum EnumName : EnumDataType { value1, value2, ...valueN }

or
enum EnumName : EnumDataType { value1=number1, value2=number2, ... valueN=numberN}



Example1 based on enum EnumName : EnumDataType { value1, value2, ...valueN }
using System;

namespace Console_Enum
{
    class Program
    {
        //declare enum datatype
        enum Direction
        {
            North,
            East,
            West,
            South
        }
        static void Main(string[] args)
        {
            //declare enum datatype variable
            Direction north = Direction.North;
            Console.WriteLine("{0}", north);
            Console.WriteLine("Enum.GetNames(typeof(Direction))");
            foreach (var item in Enum.GetNames(typeof(Direction)))
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Enum.GetValues(typeof(Direction))");
            foreach (var item in Enum.GetValues(typeof(Direction)))
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Count Values of enum: {0}",Enum.GetNames(typeof(Direction)).Length);
        }
    }
}

OUTPUT
North
Enum.GetNames(typeof(Direction))
North
East
West
South
Enum.GetValues(typeof(Direction))
North
East
West
South
Count Values of enum: 4

Example2 based on enum EnumName : EnumDataType { value1=number1, value2=number2, ... valueN=numberN}

using System;

namespace Console_Enum
{
    class Program
    {
        //declare enum datatype
        enum Direction
        {
            North=3,
            East=1,
            West=2,
            South=4
        }
        static void Main(string[] args)
        {
            //declare enum datatype variable
            Direction north = Direction.North;
            Console.WriteLine("{0}", north);
            Console.WriteLine("Enum.GetNames(typeof(Direction))");
            foreach (var item in Enum.GetNames(typeof(Direction)))
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("Enum.GetValues(typeof(Direction))");
            foreach (var item in Enum.GetValues(typeof(Direction)))
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("Count Values of enum: {0}",Enum.GetNames(typeof(Direction)).Length);
            //Acccess name based on the value
            Console.Write("Acccess name based on the value: ");
            Console.WriteLine(Enum.GetName(typeof(Direction),2));
            

        }
    }
}

OUTPUT
North
Enum.GetNames(typeof(Direction))
East
West
North
South
Enum.GetValues(typeof(Direction))
East
West
North
South
Count Values of enum: 4
Acccess name based on the value: West


Example3

using System;

namespace Console_Enum
{
    class Program
    {
        //declare your enum with its values datatype
        enum Direction: byte
        {
            North=3,
            East=1,
            West=2,
            South=4
        }
        static void Main(string[] args)
        {
            //declare enum datatype variable
            //dirVar is a variable of enum type named Direction 
            Direction dirVar = Direction.North;
            Console.WriteLine("{0}", dirVar);
            dirVar = Direction.South;
            Console.WriteLine("{0}", dirVar);
            //typecast to get the value 
            Console.WriteLine("{0}", (byte)dirVar);
            //get the array of the names of the constants in the Enum
            Console.WriteLine("=====");
            for (int i = 0; i < Enum.GetNames(typeof(Direction)).Length; i++)
            {
                Console.WriteLine(Enum.GetName(typeof(Direction),i+1));
            }
            Console.WriteLine("****Get array of names of enum ******");
            var arr = Enum.GetNames(typeof(Direction));
            foreach (var item in arr)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("### Given a sequence number of enum, get its Name ####");
            for (int i = 1; i <= Enum.GetNames(typeof(Direction)).Length; i++)
            {
                Console.WriteLine((Direction)i);
            }

            Console.WriteLine("+++++++++++++");
            foreach (var item in arr)
            {
                //parse string of enum
                //here Enum.Parse is used to parse string name of the Direction enum
                //depending upon the typecast the value is returned
                var enumValue = (byte)Enum.Parse(typeof(Direction), item);
                Console.WriteLine(item +" "+ enumValue);
            }
        }
    }
}

OUTPUT:

North
South
4
=====
East
West
North
South
****Get array of names of enum ******
East
West
North
South
### Given a sequence number of enum, get its Name ####
East
West
North
South
+++++++++++++
East 1
West 2
North 3
South 4
Example
using System;
namespace ConsoleApp3
{
    public enum Color { red, green, blue, pink } // by default int
    public enum direction : byte { east, west, north, south }
    public enum quality : byte { good=1, better=2, best=4, excellent=8 }
    public enum width { narrow=2, medium, large}
    //enum typecast to get its numeric values
    class Program
    {
        static void Main(string[] args)
        {
            //enumerate values
            foreach (Color item in Enum.GetValues(typeof(Color)))
            {
                //Console.Write((int)item + " ");//0 1 2 3
                Console.Write(item + " ");//red green blue pink
            }
            Console.WriteLine();
            foreach (var item in typeof(direction).GetEnumValues() )
            {
                Console.Write(item + " ");//east west north south
            }
            Console.WriteLine();
            foreach (var item in typeof(quality).GetEnumNames())
            {
                var result =(byte) Enum.Parse(typeof(quality), item);
                Console.Write(result.ToString() + " "); //1 2 4 8
            }
            Console.WriteLine();
            //Increment by 1 from the initial value
            foreach (var item in typeof(width).GetEnumNames())
            {
                var result = (int)Enum.Parse(typeof(width), item);
                Console.Write(result.ToString() + " "); //2 3 4
            }
            Console.ReadKey();
        }
    }
}
Example
using System;
namespace ConsoleApp3
{
    public enum Color { red=2, green, blue, pink } // by default int
    class Program
    {
        static void Main(string[] args)
        {
            //enumerate names as string array
            Type myEnumtype = typeof(Color);
            string[] enumNames = myEnumtype.GetEnumNames();
            foreach (string item in enumNames)
            {
                Console.WriteLine(item);
            }
            // IsEnumDefined()
            for (int i = 0; i < typeof(Color).GetEnumNames().Length+2; i++)
            {
                Console.WriteLine(myEnumtype.IsEnumDefined(i).ToString()); 
            }
        }
    }
}


No comments:

Post a Comment

Hot Topics