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# ?

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...
There are times when you might get the below error when you have multiple projects with-in your Visual Studio solution....