In C# we can convert an enum
to an int
by static typecasting as shown below:
int res = (int)myEnum;
Is any other way to do this conversion?
There are plenty of other ways (including Convert.ToInt32
as mentioned by acrilige), but a static cast is probably the best choice (as far as readability and performance are concerned)
Here is an example enum:
public enum Books
{
cSharp = 4,
vb = 6,
java = 9
}
Then the code snippet to use would be:
Books name = Books.cSharp;
int bookcount = Convert.ToInt32(name);
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