c# - What is the cheapest way in .net to determine enumeration-vs-number? -


i trying determine whether given string enumeration value or not. rather common string should decimal value. question whether takes longer enum.tryparse() on string or regex check decimal value or int32.tryparse(). if string not numeric (unsigned) decimal value, highly likely, not 100% guaranteed, string enum value.

this check made many many times. want lowest average cost.

you should profile code determine if problem. if not, whatever gives clear, concise (working) code. if problem, way pick approach use:

bool probablyanumber = char.isdigit(mystr[0]); 

this should work because enums can't start digits, while numbers (with exceptions "-1" , " 1 "). whether improvement or makes things worse depends on data set.

it's worth noting enums can parsed numbers, behavior of enum.tryparse may not expecting.

enum myenum {     zero,     one,     two,     ten = 10 }  myenum e; enum.tryparse<myenum>("10", out e); // success; e == myenum.ten  myenum e; enum.tryparse<myenum>("11", out e); // success; e == (myenum)11 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -