Cloud Training (Online)




How to cast integer to Enum in C# ?

This is a really simple one . Below is a simple example of an enum called “Designation” defined with the values Trainee,SoftwareEngineer,Architect.

enum Designation
{
Trainee,
SoftwareEngineer,
Architect
}

We can cast the integer value to the enum (Designation) like the way shown below.

Designation design = (Designation)2;

There are other ways by which you can cast the integer value to Enum in C# .


You can alternatively use the Enum.ToObject to cast the integer valu to the enum type.

var design1 = Enum.ToObject(typeof(Designation), 2);
MessageBox.Show(design1.ToString());
How to cast integer to Enum in C# ?
How to cast integer to Enum in C# ?