Enum as a Generics constraint - why doesn't it work?

R

Rune B

Hi Group.

I would like to do the following:


public static DecriptiveCollection GetValuesForDisplay<T>() where T :
System.Enum
{
DecriptiveCollection list = new DecriptiveCollection ();
foreach(T value in Enum.GetValues(typeof(T)))
{
// add description Attributes and enum values to collection
}
return list;
}


But I recieve an "Contstraint cannot be special class Enum" - compile error
....
- why is this?

Regards, Rune
 
M

Mattias Sjögren

- why is this?

Even if it was possible, it wouldn't work the way you probably want.
In addition to actual enum types, you would be able to specify
System.Enum itself (which isn't an enum).


Mattias
 
N

Naveen

This is by design and it has been mentioned in the specification.

C#2.0 Specification

http://download.microsoft.com/downl...e5e-f87a44af3db9/CSharp 2.0 Specification.doc

A class-type constraint must satisfy the following rules:

The type must be a class type.
The type must not be sealed.
The type must not be one of the following types: System.Array,
System.Delegate, System.Enum, or System.ValueType.
The type must not be object. Because all types derive from object, such
a constraint would have no effect if it were permitted.
At most one constraint for a given type parameter can be a class type.
 
R

Rune B

Mattias Sjögren said:
Even if it was possible, it wouldn't work the way you probably want.
In addition to actual enum types, you would be able to specify
System.Enum itself (which isn't an enum).

Yeah, I guess there's a reason for the Type.IsEnum property.

- It would still be nice to have the compile-time verification of the Types
you pass in to such a method though ...

R-)
 
J

Joanna Carter [TeamB]

"Rune B" <[email protected]> a écrit dans le message de (e-mail address removed)...

| public static DecriptiveCollection GetValuesForDisplay<T>() where T :
| System.Enum
| {
| DecriptiveCollection list = new DecriptiveCollection ();
| foreach(T value in Enum.GetValues(typeof(T)))
| {
| // add description Attributes and enum values to collection
| }
| return list;
| }
|
|
| But I recieve an "Contstraint cannot be special class Enum" - compile
error

Why do you need to use generics ?

void PrintEnum(Enum e)
{
Array a = Enum.GetValues(e.GetType());
foreach (object o in a)
...
}

....or you could do something devious like this :

void PrintEnum<T>(Enum e)
{
Array a = Enum.GetValues(typeof(T));
foreach (T o in a)
...
}

Joanna
 
R

Rune B

Joanna Carter said:
Why do you need to use generics ?

void PrintEnum(Enum e)
{
Array a = Enum.GetValues(e.GetType());
foreach (object o in a)
...
}

...or you could do something devious like this :

void PrintEnum<T>(Enum e)
{
Array a = Enum.GetValues(typeof(T));
foreach (T o in a)
...
}

I guess I don't *need* generics, I just like them ;-)
- and the constraint they can give you at compiletime.

- I ended up doing something similar to your first example.

R-)
 
G

Guest

where T : System.Enum
...
But I recieve an "Contstraint cannot be special class Enum" - compile error
....
- why is this?

The language spec does not allow this - you cannot restrict to Delegate
either (another common thing people want to do).

You can put a test in the static constructor to enforce that the Type of T
is Enum, but it will be a runtime check.

Mark
 
N

Nick Hounsome

The thing about constraints is that the name is misleading - they were
actually added not constrain the user of a class but to remove constraints
on the implementor the class!

The point of a constraint is to allow access to methods and properties of an
object of parameterized type without casting but since Enum adds nothing to
Object (all the interesting methods are static) it isn't allowed.

This seems to be a common policy in C# - whatever seems unnecessary or
redundant is explicitly prohibited - and in many cases they have carried it
too far. Personally I think you should be allowed to write stuff like "where
T: object" if you want - what's the harm? And in the case of enums I agree
that there are cases where it would be useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top