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# ?
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply