473,769 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Jun 27 '08 #1
35 2912
On Thu, 17 Apr 2008 09:31:03 -0700, PIEBALD
<PI*****@discus sions.microsoft .comwrote:
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
Jun 27 '08 #2
On Thu, 17 Apr 2008 09:57:50 -0700, Peter Duniho
<Np*********@nn owslpianmk.comw rote:
[...]
>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".
Okay...I found the C# 3.0 specification. I guess ECMA hasn't adopted it
yet? I don't see it anywhere on their web site.

Anyway, section 4.4.4 has to do with satistfying constraints when _using_
a generic. I don't see how that's relevant with respect to what
constraints you can declare for a generic.

Which brings me back to my previous question:
What is giving you the impression that the specification would allow an
enum as a type constraint?
Specifically, can you quote any text from the C# specification that
directly leads you to believe that you should be able to specify an enum
type as a constraint for a generic?

Pete
Jun 27 '08 #3
I'm looking at "Standard ECMA-334", 4th edition, June 2006. There's no

I'm looking at the "C# Language Specification Version 3.0" available at
http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx

Jun 27 '08 #4
"PIEBALD" <PI*****@discus sions.microsoft .comwrote:
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". [...] Are there reasons why these types can't or shouldn't be
supported as constraints?
This is basically because enum types aren't really inherited from the
base types, despite the colon notation. For example, if you write "enum
X : byte", it means that every member of X *is* a byte and not that X is
anything like a class derived from byte. (If you try to derive a class
from byte, you'll see that you can't possibly do so: it's a sealed
class.)
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.
An alternative would be to require a type "where T : MyEnumWrapper", and
have MyEnumWrapper be a wrapper for a single value of that particular
enum. Ugly, certainly, but you could have compile-time type safety if
you did that.

Eq.
Jun 27 '08 #5
On Thu, 17 Apr 2008 10:33:20 -0700, Paul E Collins
<fi************ ******@CL4.orgw rote:
"PIEBALD" <PI*****@discus sions.microsoft .comwrote:
>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". [...] Are there reasons why these types can't or shouldn't be
supported as constraints?

This is basically because enum types aren't really inherited from the
base types, despite the colon notation. For example, if you write "enum
X : byte", it means that every member of X *is* a byte and not that X is
anything like a class derived from byte. (If you try to derive a class
from byte, you'll see that you can't possibly do so: it's a sealed
class.)
"byte" isn't a class at all, never mind a sealed one. It's a struct
(thus, a value type):
http://msdn2.microsoft.com/en-us/system.byte.aspx

Likewise, an enum is a value type.
>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.

An alternative would be to require a type "where T : MyEnumWrapper", and
have MyEnumWrapper be a wrapper for a single value of that particular
enum. Ugly, certainly, but you could have compile-time type safety if
you did that.
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.

Pete
Jun 27 '08 #6
What is giving you the impression that the specification would allow an
enum as a type constraint?

Specifically, can you quote any text from the C# specification that
directly leads you to believe that you should be able to specify an enum
type as a constraint for a generic?
More the _absence_ of a restriction in that section.
According to http://msdn2.microsoft.com/en-us/lib...tem.enum.aspx:

"
Enum Class
Provides the base class for enumerations.
"

all enums derive from System.Enum. System.Enum _should_ satisfy section 4.4.4

"
o An implicit reference conversion (§6.1.6)
"

"
6.1.6 Implicit reference conversions
The implicit reference conversions are:
....
• From any class-type S to any class-type T, provided S is derived from T
....
"

So, it seems to me that System.Enum is a class and a particular enum derives
from it, so the language seems to allow it.

BUT, the Microsoft _implementation _ of the C# language (Visual C#) targets
..net, and in fact, the error says:

"
CS0702: Constraint cannot be special class 'System.Enum'
"

and I draw particular attention to the word "special". Of all the classes in
..net, very few are "special".

The existence of System.Enum as a base class for enums should _enable_ its
use as constraints. I see no restriction in the specification to _disallow_
it.

Jun 27 '08 #7
P.S. If not 6.1.6:

all enums derive from System.Enum. System.Enum _should_ satisfy section 4.4.4

"
o A boxing conversion (§6.1.7), provided that type A is a non-nullable value
type.
"

"
6.1.7 Boxing conversions
A boxing conversion permits a value-type to be implicitly converted to a
reference type. A boxing conversion exists from any non-nullable-value-type
to object, to System.ValueTyp e and to any interface-type implemented by the
non-nullable-value-type. Furthermore an enum-type can be converted to the
type System.Enum.
"

Jun 27 '08 #8
On Thu, 17 Apr 2008 11:16:01 -0700, PIEBALD
<PI*****@discus sions.microsoft .comwrote:
[...]
and I draw particular attention to the word "special". Of all the
classes in
.net, very few are "special".
That's correct. And the "special" is IMHO important. They are telling
you that it's not really a class. You can't inherit it, for example.
It's a façade, one that exists to make particular aspects of the type more
seamless. The type isn't _really_ a class.

An enum is still basically a value type...that is, it behaves more like a
value type than a reference type, and it's reference types that can be
used as a constraint.
The existence of System.Enum as a base class for enums should _enable_
its
use as constraints. I see no restriction in the specification to
_disallow_
it.
Well, this seems like a classic example of how biases affect our reading
of information. You want to constrain the generic to only support enums,
so of course you're predisposed to not read the information in a way that
would contradict that. On the other hand, my bias is towards explaining
how the specification adequately describes what you're seeing, so I'm
predisposed to reading the information in a way that does contradict your
position.

Sorry to say, there's really not much way around that. The fact is, you
can't constrain your type parameters to the System.Enum. You can insist
that the specification doesn't address that issue all you want, it's not
going to change how things work.

By the way, in the ECMA specification, they suggest using a static
constructor for checking the constraint, for situations where the language
doesn't support the constraint you want. That would at least ensure that
the constraint is checked only once, when you first use the class with
that specific enum type, rather than for each instance of the class you
create.

Of course, if you're trying to constrain something other than a class, I
guess that wouldn't apply. :)

Pete
Jun 27 '08 #9
my bias is towards explaining how the specification adequately describes
what you're seeing

No, the specification doesn't. Certainly the specification allows the
keywords class and struct as constraints while (regrettably) omitting enum
(and delegate and event), so perhaps one can read _that_ as a statement in
your favor. But...

From a language standpoint (not an implementation standpoint) I see no
reason to omit enum as a constraint in the first place.

If an implementer (and there _could_ conceivably be many implementations for
various platforms) chooses to implement enum as a class, then that class
_should_ be allowed as a constraint. But while Microsoft chose to use a class
to implement enums, for some unexplained reason, they also chose to make it
"special".
The fact is, you
can't constrain your type parameters to the System.Enum. You can insist
that the specification doesn't address that issue all you want, it's not
going to change how things work.
No, the specification knows nothing of System.Enum, that's merely a matter
of implementation.

The specification says that if a type has a base class you can use that base
class as a constraint. But, when implementing .net and its associated
compiler, someone decided that that shouldn't hold true for some classes even
though there appears to be no reason for that decision.
By the way, in the ECMA specification, they suggest using a static
constructor for checking the constraint, for situations where the language
doesn't support the constraint you want. That would at least ensure that
the constraint is checked only once, when you first use the class with
that specific enum type, rather than for each instance of the class you
create.
I'll take a look.
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8626
by: paul calvert | last post by:
I hope somewhere here has encountered and solved a similar problem in the past. 1) on a new Win2000 PC: installed Visual C++ 6.0 download & install single file Service Pack 5.0 2) try to build my gui and dll projects, whose project, workspace, source files all resided on network drive mapped to H. The H mapping,
10
2567
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class forgets to implement some method I normally get a compile error(if the class is created in some way of course). When using the Visual Studio 6 compiler however, it does not generate a compile error in this case: - I have a implementing class A...
2
5840
by: Mary | last post by:
Hello, I am having a problem with the cl compiler. I have written a C class (RegConnect.c) which uses Win32 API functions such as RegOpenKey, RegCloseKey etc. Initially when I was trying to create a dll, using the cl compiler I was getting many unresolved external errors, Example 1 below (8 in total, 7 to do with the registry function calls and 1 from wsprintfA call) as I hadn't included the AdvAPI32.lib file. So I created a LINK...
0
2401
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are closer to the point at which the
1
13253
by: Ayende Rahien | last post by:
reparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 53168B12): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem,
3
5267
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error (0xc0000005 at address 535F072A): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are...
4
3325
by: David Sworder | last post by:
Consider the following line of code (it's not important what it does): resp.DocItem=Relations.SelectDocItems_BySearchString(req.SearchPhrase); It turns out that this line is in error. The property 'DocItem' should be 'DocItems.' The problem is that instead of notifying me of where the problem has occurred, the compiler just crashes with an "internal error" (see bottom of this message). Now if I were to write: ...
6
2728
by: David Lack | last post by:
Hi, I recently installed a 60-day trial of .NET 2003 on my development system. I made tests with previous personal projects (which compiled ok with VC6) and some open source files, and keep facing the same problem with many of them: I keep getting errors such as: ....\WinUser.h(8028): fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 2701)
27
3076
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in the source (gcc-4.3.1/gcc/c-common.c) of gcc 4.3.1, is a syntax error. I'm inclined to agree, as it is like no C I have ever met. what is "C_COMMON_FIXED_TYPES (, fract);" supposed to mean? Could it be
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.