Enum value

I'm a little confused: is this really the only method to read the value from Enum-Code?

(int)Enum.Parse(typeof(MyEnum), MyEnumCode.ToString()) 

Something important and not the best way to get value?

+4
source share
5 answers

I don’t know what you mean by “Enum-Code”, but why not just convert it to int?

 int value = (int)MyEnum.MyEnumCode; 
+15
source

No, you can just simply overlay on an int:

 (int)MyEnum.MyEnumCode 

Working out a bit. The internally enumeration is actually an int. Therefore, the throw is free. But it also means that you can easily have values ​​in your listing that don't exist. For instance.

 MyEnum val = (MyEnum)-123544; 

Perfectly valid code.

+7
source

What do you want to achieve? Do you want to get the integer value associated with the enumeration? You can just translate enum to int ...

 (int)MyEnum.MyEnumCode; 
+2
source

Try the following:

 int x = MyEnumCode as int; string y = MyEnumCode.ToString(); int z = (int)MyEnumCode; 
+1
source

What about

 (int)MyEnumCode? 

The main type for listing is int by default, so you can just distinguish it.

+1
source

Source: https://habr.com/ru/post/1341783/


All Articles