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?

Community
  • 1
  • 1
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
  • 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](http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum). And yet another dupe: http://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint – Darin Dimitrov Sep 24 '12 at 05:42
  • 1
    its already answered here http://stackoverflow.com/questions/1331739/enum-type-constraints-in-c-sharp – prashanth Sep 24 '12 at 05: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](http://stackoverflow.com/questions/1331739/enum-type-constraints-in-c-sharp). – Raheel Khan Sep 24 '12 at 05: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: http://msmvps.com/blogs/jon_skeet/archive/2009/09/11/1722426.aspx – Darin Dimitrov Sep 24 '12 at 05: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. – Darin Dimitrov Sep 24 '12 at 05:51
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Sep 24 '12 at 06:14

1 Answers1

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."

Community
  • 1
  • 1
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • 2
    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. – supercat Jan 18 '13 at 23:00
  • 1
    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. – supercat Jan 18 '13 at 23:02