Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rune B - 26 Mar 2006 13:40 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.