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 enum
s 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
Post a Comment