Test if an object is an Enum

C#EnumsValue Type

C# 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAran MulhollandView Question on Stackoverflow
Solution 1 - C#EMPView Answer on Stackoverflow
Solution 2 - C#Chris SchmichView Answer on Stackoverflow
Solution 3 - C#LaramieView Answer on Stackoverflow
Solution 4 - C#bugged87View Answer on Stackoverflow