Get the int value from the enum in C#
Get the int value from the enum in C#
Student
I am Utpal Vishwas from Uttar Pradesh. Have completed my B. Tech. course from MNNIT campus Prayagraj in 2022. I have good knowledge of computer networking.
Sure, here's how you can get the int value from an enum in C#:
C#
In this example, the
myEnumvariable is an enum that is initialized to the value ofValue1. The(int)myEnumcast operator casts themyEnumvariable to an int. The value of themyIntvariable is now 1.You can also use the
Enum.ToInt32()method to get the int value from an enum. Here's how you can do it:C#
In this example, the
myEnumvariable is an enum that is initialized to the value ofValue1. TheEnum.ToInt32(myEnum)method casts themyEnumvariable to an int. The value of themyIntvariable is now 1.Which method you use depends on your specific needs. If you need to cast an enum to an int and you don't need to do any further processing on the int value, then the cast operator is a good option. If you need to do further processing on the int value, then the
Enum.ToInt32()method is a good option.