100

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

 foreach (var item in Enum.GetValues(theObject.GetType())) {

     //do something
 }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

4 Answers4

229

The question is the answer. :)

bool isEnum = theObject is Enum;
EMP
  • 59,148
  • 53
  • 164
  • 220
75

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

bool isEnum = theObject.GetType().IsEnum;
John Cummings
  • 1,949
  • 3
  • 22
  • 38
Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
10

just use

if (theObject is Enum)
 //is an enum
Laramie
  • 5,457
  • 2
  • 39
  • 46
6

For generic type parameters, the parameter can be constrained rather than tested:

where T : Enum
bugged87
  • 3,026
  • 2
  • 26
  • 42
  • C# 7.3! that's a really good reason to update C# version :) project properties->build->Advanced – ephraim Oct 25 '21 at 09:52