Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if an object is an Enum

I would like to know if 'theObject' is an enum (of any enum type)

 foreach (var item in Enum.GetValues(theObject.GetType())) {       //do something  } 
like image 293
Aran Mulholland Avatar asked May 27 '10 06:05

Aran Mulholland


People also ask

How do you check if a property is an enum?

In C#, we can check the specific type is enum or not by using the IsEnum property of the Type class. It will return true if the type is enum. Otherwise, this property will return false. It is a read-only property.

Is nullable enum?

Enum types cannot be nullable.


2 Answers

The question is the answer. :)

bool isEnum = theObject is Enum; 
like image 91
EMP Avatar answered Oct 08 '22 14:10

EMP


If you have a Type, use the Type.IsEnum property, e.g.:

bool isEnum = theObject.GetType().IsEnum; 
like image 37
Chris Schmich Avatar answered Oct 08 '22 14:10

Chris Schmich