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?

4
  • 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) Commented Jun 8, 2011 at 22:47
  • did you read this Q? stackoverflow.com/questions/79126/… Commented Jun 8, 2011 at 22:47
  • Take a look at this: msmvps.com/blogs/jon_skeet/archive/2009/09/10/… Commented Jun 8, 2011 at 22:53
  • Wanted something very simple and got it. thanks mates :-) Commented Jun 8, 2011 at 22:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Simple and straight forward, thanks Ed :-)
These are now supported in C# 7.3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.