I have the following enum declaration:
enum MyType
{
Boolean,
Int,
Double,
String
}
Then if I have a string abc = "anyvalue", how can I check whether the abc value is either bool, int, double or string corresponding to a myType enumerated value?
You can use
Enum.TryParsemethod. This will return true if the value was successfully parsed into one of the enumeration values while also outputting the parsed enumeration value.If you want to ignore case when performing the parsing use the overload that accepts a boolean parameter.
It’s also possible to use
Enum.IsDefinedbut this method always does a case-sensitive search so it is less flexible thanEnum.TryParseand can only be used to know if the name is defined or not.