Convert int to enum in C#
Convert int to enum in C#
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Ravi Vishwakarma
11-Jun-2024You can convert an int value to an enumeration (enum) type using type casting or the Enum.Parse method.
Here's how you can do it:
Type Casting
You can directly cast an int to the enum type if the integer value corresponds to one of the defined enum values.
Enum.Parse Method
If you need to convert an int value to an enum and you're not sure if the integer value corresponds to a valid enum value, you can use the Enum.Parse method.
Convert.ChangeType Method
You can also use the Convert.ChangeType method if you prefer.