By 22/07/2015 38 Comments

C# Enumeration

  • An enumeration in C# is a collection/list of integer constants which has different meaningful names/identifiers and it is created by a keyword “enum”.
  • Each name represents an integer type value. The names defined in an enumeration are called symbols or identifiers which are separated by comma and called an enumeration-list.
  • Since an enumeration is an integer value/constant but no implicit conversions can be defined between an enumeration type and integer values, to achieve the conversions an explicit cast must be used between built in integer types and enumeration types.
  • An enumeration can be used as a control variable in “switch” and loops.
  • Like an array symbols / names in an enumeration start from 0 by default, it means the first enumeration symbol / name has value 0 and so on. We can also assign custom integer values into one or more enumeration symbols through an assignment (=) operator in enumeration-list.
  • Enumerations are depended on an “integer” type by default but it can be created of any data type except the data types char, string, bool and floating points.
  • In .NET framework all enumerations are implicitly inherit System.Enum namespace, which inherits System.ValueType namespace.
  • Each enumeration member can be accessed through its type name by dot (.) operator.
  • Enumeration plays an important role in real world programs. It saves our time, gives a clear understand of code to programmers, it sends user command options to an application in meaningful string names instead of 1, 2 etc.
  • An enumeration can be a member of a namespace or class.

General Syntax:

access-modifier enum enumName
{
       enumeration list
};

STATEMENT DESCRIPTION
access-modifier It can be public, private or protected etc. It is an optional.
enum The keyword enum which indicates that this is an enumeration declaration.
enumName Name of enumeration. It should be any meaningful name.
{  enumeration list }; Enumeration body where an enum list is existed.

Example – 1: Enumeration using for loop and explicit casting:

using System;
namespace csharpBasic
{
    class SearchEngines
    {
        // A public enumeration “Names” is declared.
        public enum Names
        {
            Google,
            Bing,
            Yahoo,
            Ask
        };
    }

    class Program
    {
        static void Main(string[] args)
        {
            /* A reference variable “names” is declared of an enumeration type. 
               Notice the following class calls an enumeration through its name (SearchEngines) 
               followed by dot (.) operator.*/
            SearchEngines.Names names;

            /* for loop is looping through each symbol/enum names 
               from an enumeration-list and print them.*/
            for (names = SearchEngines.Names.Google; names <= SearchEngines.Names.Ask; names++)
                Console.WriteLine((int)names + " " + names);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        0 Google
        1 Bing
        2 Yahoo
        3 Ask
    
       */
    }
}

Example – 2: Enumeration of long data type using for loop and explicit casting:

using System;
namespace csharpBasic
{
    class SearchEngines
    {
        // A public long type enumeration “Names” is declared.
        public enum Names:long
        {
            Google,
            Bing,
            Yahoo,
            Ask
        };
    }

    class Program
    {
        static void Main(string[] args)
        {
            /* A reference variable “names” is declared of an enumeration type. 
               Notice the following class calls an enumeration through its name (SearchEngines) 
               followed by dot (.) operator.*/
            SearchEngines.Names names;

            /* for loop is looping through each symbol/enum names 
               from an enumeration-list and print them.*/
            for (names = SearchEngines.Names.Google; names <= SearchEngines.Names.Ask; names++)
                Console.WriteLine((long)names + " " + names);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        0 Google
        1 Bing
        2 Yahoo
        3 Ask
    
       */
    }
}

Example – 3: A real world program:

using System;
namespace csharpBasic
{
    class MediaPlayer
    {
        // An enumeration-list declaration with different value constants.
        public enum Controls
        {
            Play,
            Pause,
            Stop,
            Next,
            Previous,
            Equilizer
        };

        /* void type method declaration which pass an enumeration 
           type reference variable as its parameter. */
        public void PlayerControls(Controls controls)
        {
            // Switch statement declaration.
            switch (controls)
            {
                case Controls.Play:
                    Console.WriteLine("It is played..");
                    break;

                case Controls.Pause:
                    Console.WriteLine("It is paused.");
                    break;

                case Controls.Next:
                    Console.WriteLine("Next track..");
                    break;

                case Controls.Previous:
                    Console.WriteLine("Previous track..");
                    break;

                case Controls.Stop:
                    Console.WriteLine("It has stoped.");
                    break;

                case Controls.Equilizer:
                    Console.WriteLine("Equalizer is on..");
                    break;

                default:
                    Console.WriteLine("No control is selected !");
                    break;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
         // Media player object is being created.
            MediaPlayer mediaPlayer = new MediaPlayer();

         /* Following statements call the PlayerControls() method 
            through an object reference and pass different enumeration values.*/
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Play);
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Pause);
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Next);
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Previous);
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Equilizer);
            mediaPlayer.PlayerControls(MediaPlayer.Controls.Stop);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        It is played..
        It is paused.
        Next track..
        Previous track..
        Equalizer is on..
        It has stoped.
       
       */
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .