Shamim Hafiz Shamim Hafiz - 1 month ago 15
C Question

How to get the Enum Index value in C#

In C,

enums
, internally equates to an integer. Therefore we can treat data types of
enum
as integer also.

How to achieve the same with C#?

Answer

Simply cast it to an int, like below:

int eValue = (int)enumValue;

Here's another example that shows you how to set up their internal integer values:

public enum MyEnum
{
    MyValue1 = 1,
    MyValue2 = 2
}

(int)MyEnum.MyValue2 == 2; // True