Topic : Enumerations |Enums Keyword| in C# Programming Language:

Enumerations:

An enumeration, also called an enumerated type or enum , is a way to create a special numeric data type restricted to a set of named constants with meaningful names for those values. Enumeration provides efficient way to assign multiple constant integral values to a single variable. Enumeration improves code clarity and makes program easier to maintain. Enumeration in C# also provides more security by better error-checking technology and compiler warnings.



Declaring enum Variable:

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: The Basic Syntax is:



enum Variable_name 
{
   /*enumeration
     list */
};


  • In Above Syntax:

    » enum is the keyword, used to declare an Enumeration.
    » The Variable_name specifies the enumeration type name.
    » The enumeration-list is a comma separated list of identifiers.



Each of the Symbols in the enumerations list stands for an integer value. By Default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example:


/*Example: Enums: InfoBrohter:*/

using System;

namespace Enumeration
{
    class Program
    {
        public enum days  //declaring enums:
        {
            Monday,     //0
            Tuesday,    //1
            Wednesday,  //2
            Thursday,   //3
            Friday,     //4
            Saturday,   //5
            Sunday      //6
        }


        static void Main(string[] args)
        {
            int start = (int)days.Monday;
            int end = (int)days.friday;

            Console.WriteLine("Monday({0}) is the first day of the week:", start);
            Console.WriteLine("Friday({0}) is the Last day of the week:", end);

            Console.ReadKey();
        }
    }
}


Each enumerator ends with a comma, but the comma after the last value is optional. Including this last comma makes rearrangement and adding new values at a later time easier. But depending on your personal preference it can also make the code look sloppy and give the implicit impression that a value is missing. Either way, the programming code will compile.



An explicit cast is necessary to convert from enum type to an integral type.


Enumerators can use initializers to override the default values. as shown in the following Example:


 public enum days  //declaring enums:
 {
    Monday = 1,     
    Tuesday,    
    Wednesday,  
    Thursday,   
    Friday,     
    Saturday,   
    Sunday      
 }


In Above Example, the Sequence of elements is forced to start from 1 instead of 0, however including a constant that has the value of 0 is recommended.

Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type, as shown in the following example.


 public enum days:long  //declaring enums of type long:
 {
    Monday = 1,     
    Tuesday,    
    Wednesday,  
    Thursday,   
    Friday,     
    Saturday,   
    Sunday      
 }


The Approved types of an enum are:
ByteSbyteshortushort
intuintlongulong


  • More About Enum:

    » enums are enumerated data type in c#.
    » enums are not for end-user, they are meant for developers.
    » enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
    » Enumerations (enums) make your code much more readable and understandable.
    » enum values are fixed. enum can be displayed as a string and processed as an integer.
    » The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
    » Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
    » Enums are value types and are created on the stack and not on the heap.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement