Enumeration (enum) in C#

C# Enum: Here, we will find out about the enum in C#, how to announce, get to enumeration in C#?

enum represents enumeration, it is a lot of named whole number constants, their default number qualities start with 0, we can likewise set some other grouping of the qualities.

An enum is characterized by utilizing the enum watchword.

Syntax:

    enum enum_name {enumeration_list };

Example 1:

Here, we are characterizing an enum named hues with the three constants RED, GREEN and BLUE, we are not instating them with any worth. Along these lines, consistent will have values 0, 1 and 2.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum colors { RED, GREEN, BLUE };
        static void Main(string[] args)
        {            
            //printing values
            Console.WriteLine("Red: {0}", (int)colors.RED);
            Console.WriteLine("Green: {0}", (int)colors.GREEN);
            Console.WriteLine("Blue: {0}", (int)colors.BLUE);
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

Red: 0
Green: 1
Blue: 2

Example 2:

Here, we are characterizing an enum named hues with the three constants RED, GREEN and BLUE, we are instating RED with 10, GREEN with 20 and BLUE isn’t getting introduced with any worth, so it will take 21. Therefore, steady will have values 10, 20 and 21.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum colors { RED = 10, GREEN = 20, BLUE };
        static void Main(string[] args)
        {            
            //printing values
            Console.WriteLine("Red: {0}", (int)colors.RED);
            Console.WriteLine("Green: {0}", (int)colors.GREEN);
            Console.WriteLine("Blue: {0}", (int)colors.BLUE);
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

Red: 10
Green: 20
Blue: 21

Example 3: Printing enum’s steady name and worth utilizing foreach circle:

Here, we are utilizing Enum.GetValues(typeof(Days) which restores a variety of all enum values and to get the name of enum, we are utilizing Enum.GetName(typeof(Days), day). Here, Days is the enum name and day is the record.

using System;
using System.Text;

namespace Test
{
    class Program
    {
        //declaring enum
        enum Days { SUN, MON, TUE, WED, THU, FRE, SAT };
        static void Main(string[] args)
        {
            //printing enum using foreach loop
            foreach (int day in Enum.GetValues(typeof(Days)))
            {
                Console.WriteLine("Name: {0}, Value: {1}", Enum.GetName(typeof(Days), day), (int)day);
            }
			
            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

Name: SUN, Value: 0
Name: MON, Value: 1
Name: TUE, Value: 2
Name: WED, Value: 3
Name: THU, Value: 4
Name: FRE, Value: 5
Name: SAT, Value: 6

Leave a Comment

error: Alert: Content is protected!!