Get the int value from the enum in C#
Get the int value from the enum in C#
478
21-Aug-2023
Aryan Kumar
23-Aug-2023Sure, 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.