Sunday, May 27, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...