Compiler Error CS0702



I'd really like to be able to constrain a generic type to System.Enum or,
better, enum. But of course that results in "Compiler Error CS0702".

So far I've been checking the type parameter at runtime and throwing an
exception if the provided type is not an enum. That works, but it just
doesn't seem quite right.

After reading through section 4.4.4 of the C# 3.0 spec I see no reason why
constraining to System.Enum should not be allowed.

So my questions are:
Are there reasons why these types can't or shouldn't be supported as
constraints?
(If so, the documentation of Compiler Error CS0702 ought to state them.)
Is this simply a limitation of the compiler rather than of the language?
Are efforts being to made to support these types as constraints?

.



Relevant Pages

  • Re: Compiler Error CS0702
    ... But of course that results in "Compiler Error CS0702". ... exception if the provided type is not an enum. ... And it describes the allowable constraints to clearly exclude enums or other value types. ... Well, I think the most obvious reason is that since value types can't inherit other value types, if you're constraining the class to allow only one specific value type, then there's no need for the class to be generic in the first place. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... But of course that results in "Compiler Error ... supported as constraints? ... For example, if you write "enum ... Even more so than a generic constrained to just a single type, I don't see what the value in a generic constrained to a single value of a single type would be. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why need constraints?
    ... will cause a compiler error. ... the constraints on the type arguments are implicit based on the ... ..NET generics aren't instantiated at compile time - instead they're ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compiler Error CS0702
    ... But of course that results in "Compiler Error ... supported as constraints? ... This is basically because enum types aren't really inherited from the ... have MyEnumWrapper be a wrapper for a single value of that particular ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: CLP/FD: Sicstus vs Eclipse vs SWI
    ... constraints, such as alldifferent, element, and cumulative, and ... B-Prolog is not open source and there is no community supporting it. ... B-Prolog and provide paid support. ... Based on your criteria surely no products would exist today:) ...
    (comp.lang.prolog)

Loading