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


x

Interesting BlogPosts to read

How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...
How to convert Array to List in C#?
Want to convert array of strings to List of strings in C# ? . Below is a sample code to illustrate t...