I'm little bit confused: Is this really the only method to read a value from an Enum-Code?
(int)Enum.Parse(typeof(MyEnum), MyEnumCode.ToString())
Something such essential and no better way to get the value?
I don't know, what you mean by "Enum-Code", but why not just convert it to an int?
int value = (int)MyEnum.MyEnumCode;
No, you can just cast to int directly:
(int)MyEnum.MyEnumCode
Elaborating a bit. Internally an enum is actually an int. Therefore the cast is free. But it also means that you can easily have values in your enum that doesn't exist. E.g.
MyEnum val = (MyEnum)-123544;
Is perfectly valid code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With