Re: Compiler Error CS0702
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 17 Apr 2008 09:57:50 -0700
On Thu, 17 Apr 2008 09:31:03 -0700, PIEBALD <PIEBALD@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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.
I guess I'm a bit confused about the relationship between C# version and specification version.
I'm looking at "Standard ECMA-334", 4th edition, June 2006. There's no "section 4.4.4". The document I'm looking at describes generic constraints on page 405, section 25.7. And it describes the allowable constraints to clearly exclude enums or other value types. The only primary constraints allowed are an actual reference (class) type, or the keywords "class" or "struct".
What is giving you the impression that the specification would allow an enum as a type constraint?
So my questions are:
Are there reasons why these types can't or shouldn't be supported as
constraints?
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. You'd only ever be able to use that generic class with one specific type.
Since an enum is a value type, it would fall into that same reasoning.
(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?
See above. As I read it, this is clearly described as part of thelanguage.
Are efforts being to made to support these types as constraints?
Not to my knowledge. I don't see why any effort would be made, given the above.
Pete
.
- Follow-Ups:
- Re: Compiler Error CS0702
- From: PIEBALD
- Re: Compiler Error CS0702
- From: Peter Duniho
- Re: Compiler Error CS0702
- References:
- Compiler Error CS0702
- From: PIEBALD
- Compiler Error CS0702
- Prev by Date: Accesing SQL View form C# asp.net
- Next by Date: Re: Serialize XML is broken?
- Previous by thread: Compiler Error CS0702
- Next by thread: Re: Compiler Error CS0702
- Index(es):
Relevant Pages
|
Loading