2

Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#

Consider the following class:

public class Transition<TState> { public Transition () { if (!typeof(TState).IsEnum) throw (new ArgumentException("[TState] has to be of type [System.Enum].")); } } 

Ideally, this should be declared as:

public class Transition<TState> where TState: System.Enum { } 

The above, of course, generates a compile-time error. My question is why has that been made illegal. Most sources explain say that it is illegal but do not explain why. Any thoughts?

6
  • 2
    This topic has already been discussed many times. Please search before posting a question. Possible duplicate of Create Generic method constraining T to an Enum. And yet another dupe: stackoverflow.com/questions/7244/… Commented Sep 24, 2012 at 5:42
  • 1
    its already answered here stackoverflow.com/questions/1331739/… Commented Sep 24, 2012 at 5:47
  • @DarinDimitrov: They are not duplicates of those two questions. It is, in fact a duplicate of the one Rafal pointed out. I am not looking for a work-around. Just seeking the reason behind it. EDIT: The link to that question is here. Commented Sep 24, 2012 at 5:47
  • 1
    @RaheelKhan, this feature is implemented at the CIL level, it's just not implemented in C#. If you read my second dupe link you will see that there's a workaround as illustrated by Jon Skeet who wrote a nice helper. Take a look at his blog post: msmvps.com/blogs/jon_skeet/archive/2009/09/11/1722426.aspx Commented Sep 24, 2012 at 5:49
  • I know that it helps. If you had searched before posting your question that would be the first or second hit on google. Next time please do so. Commented Sep 24, 2012 at 5:51

1 Answer 1

3

As Eric Lippert says that and I quote

ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team."

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

2 Comments

Had the C# team not taken any deliberate action with regard to System.Enum's usability as a type constraint, specifying T:System.Enum would probably have worked for the purposes of restricting the types which could be passed to be either System.Enum or enumerated types, but would not have allowed the class to do anything with a T that it couldn't do with System.Enum. The C# team apparently decided that since someone who declared T:enum might want features T couldn't provide, they should add code to expressly forbid such a constraint.
The ability to specify a System.Enum constraint was not a feature that started out unimplemented; the ability to specify a System.Enum constraint for the limited purpose of compile-time validating argument types was a feature which, while it would have been less useful than ideal, would have been implemented "for free" but for a decision to actively block it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.