Assign value to enum in c#

You can assign the integer value to enum by casting it to the respective enum type.

For example , In the below sample program , you can assign the value 10 to enum by casting the value to the Designation type.

static void Main(string[] args)
{
            Designation architect1 = (Designation)10;
            Console.WriteLine(((int)architect1).ToString());
            Console.ReadLine();
}
enum Designation
{
            SoftwareEngineer = 1 ,
            TraineeEngineer = 2 ,
            Architect = 10
}
%d bloggers like this: