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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...