I want to write a utility wherein can validate a given value (string) against possible values of enum. And in case of match, return the enum instance, else return a default value. PS: I don't want to user Try...catch here.
public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue){
TEnum enumValue;
if(!TryParse(typeof(TEnum),strEnumValue, out enumValue)){ //Looking for implementation??????
return defaultValue;
}
return enumValue;
}
I am looking for some optmized implementation of "TryParse..." here. No try...catch or Enum.GetNames() solution, please.
[Edit] I know about Enum.Parse(typeof(TEnum), strEnumValue) method. It throw ArgumentException if strEnumValue is not valid.
Looking for TryParse........
Thanks