Home » C#.Net

Enumeration (enum) in C#

C# Enum: Here, we are going to learn about the enum in C#, how to declare, access enumeration in C#?
Submitted by IncludeHelp, on February 12, 2019

enum stands for enumeration, it is a set of named integer constants, their default integer values start with 0, we can also set any other sequence of the values.

An enum is defined by using the enum keyword.

Syntax:

    enum enum_name {enumeration_list };

Example 1:

Here, we are defining an enum named colors with the three constants RED, GREEN and BLUE, we are not initializing them with any value. Thus, constant 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 defining an enum named colors with the three constants RED, GREEN and BLUE, we are initializing RED with 10, GREEN with 20 and BLUE is not getting initialized with any value, so it will take 21. Thus, constant 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 constant name & value using foreach loop

Here, we are using Enum.GetValues(typeof(Days) which returns an array of all enum values and to get the name of enum, we are using Enum.GetName(typeof(Days), day). Here, Days is the enum name and day is the index.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.