I can not choose between two conversion methods. What is the best practice by converting from enum to int
1
public static int EnumToInt(Enum enumValue) { return Convert.ToInt32(enumValue); }
2:
public static int EnumToInt(Enum enumValue) { return (int)(ValueType)enumValue; }
In addition to @dtb
You can specify the int (or flag) of your enum by putting it after the equal sign.
enum MyEnum { Foo = 0, Bar = 100, Baz = 9999 }
Greetings
If you have an enumeration, for example
enum MyEnum { Foo, Bar, Baz, }
and the meaning of this listing, such as
MyEnum value = MyEnum.Foo;
then the best way to convert the value to int is
int
int result = (int)value;
I would choose the third option in the mix as an extension method on Enum
public static int ToInt(this Enum e) { return Convert.ToInt32(e); } enum SomeEnum { Val1 = 1, Val2 = 2, Val3 = 3, } int intVal = SomeEnum.ToInt();
Source: https://habr.com/ru/post/957005/More articles:Can I change the grid of the Gnome 3.10 workspace? - ubuntuIs it possible to create a dictionary "template"? - pythonGambler Ruin ASCII Patch in Java - javaHow to filter a row based on any column data with one text field - angularjsCreate a maven project as part of an SBT build - scalaSynEdit: How to make background selection of several text areas defined by start and end positions? - delphiMatplotlib output does not display in string format on IPython laptop, despite the built-in --pylab option - pythongoogle app engine project: Server error. The server detected an error and could not fulfill your request. - google-app-enginepyramid-mode electric couples and triple quotes - pythoniOS - the right approach to insert data into mySql database - phpAll Articles