Sunday 27 May 2018

c# - Enum to int best practice




I can't select between two methods of converting. 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;
}

Answer



In addition to @dtb



You can specify the int (or flag) of your enum by supplying it after the equals sign.




enum MyEnum
{
Foo = 0,
Bar = 100,
Baz = 9999
}


Cheers



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...