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
Mattias Sjögren - 26 Mar 2006 14:23 GMT
>- 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

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Rune B - 26 Mar 2006 14:38 GMT
> >- 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).
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-)
Naveen - 26 Mar 2006 14:32 GMT
This is by design and it has been mentioned in the specification.
C#2.0 Specification
http://download.microsoft.com/download/8/1/6/81682478-4018-48fe-9e5e-f87a44af3db
9/CSharp%202.0%20Specification.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.
Joanna Carter [TeamB] - 26 Mar 2006 16:28 GMT
| public static DecriptiveCollection GetValuesForDisplay<T>() where T :
| System.Enum
[quoted text clipped - 8 lines]
|
| 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

Signature
Joanna Carter [TeamB]
Consultant Software Engineer
Rune B - 26 Mar 2006 19:37 GMT
> Why do you need to use generics ?
>
[quoted text clipped - 13 lines]
> ...
> }
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-)
MarkT [developmentor] - 26 Mar 2006 23:49 GMT
> 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
Nick Hounsome - 27 Mar 2006 10:43 GMT
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.
> Hi Group.
>
[quoted text clipped - 16 lines]
>
> Regards, Rune