An enumeration is a special kind of value type limited to a restricted and unchangeable set of numerical values. Enums are strongly typed constants. They are essentially unique types that allow you to assign symbolic names to integral values.


 


Advantages of Enums:-



  • In the C# tradition, they are strongly typed, meaning that 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.

  • By default, these numerical values are integers, but they can also be longs, Bytes, etc.

  • When you define an enumeration you provide literals which are then used as Constants for their corresponding values.

  • Values specified in enumeration are static.

  • These values are final and you can't change the value at runtime.

  • The System.Enum BCL type is the base class of enum types and contains methods that allow you to work with enums in different ways, such as working with a list of names or values, converting from value to name, and converting from name to value.


 


Syntax


enum enumname { //values}


Example:-


enum DayOfWeek {


Monday=2,


Tuesday,


Wednesday,


Thursday=10,


Friday,


Saturday,


Sunday


}


Note:-



  • The first literal, is unassigned then it value is set to 0.

  • For any other literal, if the value is unassigned then the value set is greater than the value of the preceding literal.

  • By this the value of Monday is set to 0 and the value of Sunday is 6.

  • We can also specify the value to the literal in the enums.


Note:- Here the value will be Monday-2, Tuesday-3, Wednesday-4 and Thursday-10, Friday-11, Saturday-12, Sunday-13.


 


Example: Demonstrate Enums


 




 


 


 


ClsEnum16.cs


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace cSHARPEXAMPLES


{


 enum DayOfWeek {


    Monday,


    Tuesday,


    Wednesday,


    Thursday,


    Friday,


    Saturday,


    Sunday


    }


 enum ValuesEnum : byte


  {


    One = 1,


    two,


    three=5,


    four,


    five


  }


 


class ClsEnum16


{


    public String GetvalueEnteredByUser( int enteredValue) {


    //user entered value


        ValuesEnum valuesEnum = ( ValuesEnum )enteredValue;


        String str = "" ;


    switch (valuesEnum)


        {


        case ValuesEnum .One: str = "FIRST" ;


            break ;


        case ValuesEnum .two: str = "SECOND" ;


            break ;


        case ValuesEnum .three: str = "THIRD" ;


            break ;


        case ValuesEnum .four: str = "FOURTH" ;


            break ;


        case ValuesEnum .five: str = "FIFTH" ;


            break ;


        default : str = "default" ; break ;


        }


    return str;


    }


 


    public String CreateAndUseEnumType(){


        DayOfWeek dayofWeek = DayOfWeek .Thursday;


    //DayOfWeek dayofWeek = (DayOfWeek)4;


        String strDay = "" ;


    switch (dayofWeek)


        {


        case DayOfWeek .Monday:  strDay = "Monday" ;


            break ;


        case DayOfWeek .Tuesday:  strDay = "Tuesday" ;


            break ;


        case DayOfWeek .Wednesday: strDay = "Wednesday" ;


            break ;


        case DayOfWeek .Thursday: strDay = "Thursday" ;


            break ;


        case DayOfWeek .Friday: strDay = "Friday" ;


            break ;


        case DayOfWeek .Saturday: strDay = "Saturday" ;


            break ;


        case DayOfWeek .Sunday: strDay = "Sunday" ;


            break ;


        }


    return strDay;


    }


  }


}


Form16.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace cSHARPEXAMPLES


{


  public partial class Form16 : Form


    {


    public Form16()


        {


            InitializeComponent();


        }


    private void button1_Click( object sender, EventArgs e)


        { //Create and use Enums


      ClsEnum16 obj = new ClsEnum16 ();


      MessageBox .Show(obj.CreateAndUseEnumType());


        }


 


     private void button2_Click( object sender, EventArgs e)


        { //Enter Enum Value From User


 


            int _userEnterValue = Int32 .Parse(textBox1.Text);


      ClsEnum16 obj = new ClsEnum16 ();


      MessageBox .Show(obj.GetvalueEnteredByUser(_userEnterValue));


        }


    }


}


 


Output


Click On: Enter Enum value from user Button


 


 


Click On: Create and Use Enums Type


 


 

                    

Copyright © 2012 VisualBuilder. All rights reserved