Test if an object is an Enum
C#EnumsValue TypeC# Problem Overview
I would like to know if 'theObject' is an enum (of any enum type)
foreach (var item in Enum.GetValues(theObject.GetType())) {
//do something
}
C# Solutions
Solution 1 - C#
The question is the answer. :)
bool isEnum = theObject is Enum;
Solution 2 - C#
If you have a Type
, use the Type.IsEnum
property, e.g.:
bool isEnum = theObject.GetType().IsEnum;
Solution 3 - C#
just use
if (theObject is Enum)
//is an enum
Solution 4 - C#
For generic type parameters, the parameter can be constrained rather than tested:
where T : Enum