1

Possible Duplicate:
Enum type constraints in C#

Could somebody kindly explain to me with a simple sample and simple words why enum constraints are not allowed on a generic type i.e. What would happen if they were possible?

Community
  • 1
  • 1
Idrees
  • 711
  • 1
  • 7
  • 31
  • 1
    An enum is a value type, there aren't many constraints you could apply to a value type. (only `struct` and `new()` comes to mind) – Jeff Mercado Jun 08 '11 at 22:47
  • did you read this Q? http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum – Mike Miller Jun 08 '11 at 22:47
  • Take a look at this: https://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx – eulerfx Jun 08 '11 at 22:53
  • Wanted something **very** simple and got it. thanks mates :-) – Idrees Jun 08 '11 at 22:53

1 Answers1

2

Simply put: because they're not. That's how the language is designed (you wanted simple, right? :) )

enum is a value type, so you could put a restraint on your generic type where T : struct and use Type.IsEnum to check in the constructor, throwing an exception if it returns false.

Ed S.
  • 122,712
  • 22
  • 185
  • 265