Hello to all.

I came across the fact that in order to convert enum to int it is necessary to perform an explicit conversion. It works, but somehow uncomfortable.

Is it possible to somehow automate the task or make it more visually "beautiful"? I do not want to write (int) enumVar every time to convert to int

  • If explicit conversion was not needed, you would have compiled the code Cars.BMW * Cars.Audi - 4 * Dogs.Terrier . - VladD

1 answer 1

You can make an extension method:

 static class Extensions { public static int ToInt(this MyEnum value) { return (int)value; } } 

Then use:

 enumVar.ToInt() 

But personally, I do not think this is “more beautiful” and would still use a type conversion.