Scott on Writing

Musings on technical writing...

Friday Quiz: Question on .NET 2.0 Generics

Here's a little question that has me stumped, perhaps someone out there in the intraweb can answer this for me.

With Generics in 2.0 you can impose constraints on the type that can be specified by the developer.  For example, I could create a class:

class Foo<T> where T : Bar
{
   ...
}

This would only allow developers to create a Foo instance where the type specified via the Generic was of type Bar, or derived from Bar.  Ok, great.  Now imagine that I have a class SuperFoo<U>, and I want U to be of type Foo<T>, or a class derived from Foo<T>.  That is, I want to be able to say later:

SuperFoo<Foo<int>> blah;

How do I specify that?  If I try:

class SuperFoo<S> where S : Foo

I get an error because Foo is not a type, Foo<T> is the type.  If I try:

class SuperFoo<S> where S : Foo<X>

It barfs because X is an undefined type.  Finally, if I try:

class SuperFoo<S> where S : Foo<S>

It compiles the class in this instance, but I can't create a type of this class.  That is, if I do:

SuperFoo<Foo<int>> blah;

It says that Foo<int> isn't convertible to Foo<Foo<int>>, which seems to make sense since in the class definition, S is Foo<int>, so if S must be of type Foo<S> then it must be Foo<Foo<S>>.

So does anyone know how to define a class that uses generics to constrain the generic type so that the type is a generic type itself?

posted on Friday, August 20, 2004 9:26 AM

Feedback

# re: Friday Quiz: Question on .NET 2.0 Generics 8/20/2004 9:59 AM Rob Chartier


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/csharp_generics.asp


public class LinkedList<K,T> where K : IComparable<K>
{...}

There was also a best practices document that was pumped onto someone's blog (MS Employee), I belive on weblogs.asp.net, which explained why what you are trying to do is not usually considered a good idea. Something about keeping the interface for your API simple and easy to understand.



# re: Friday Quiz: Question on .NET 2.0 Generics 8/23/2004 6:18 AM Rodrigo Jordao

In order to create an instance of

class SuperFoo<S> where S : Foo<X>

you should be able to specify X in the creation expression:

SuperFoo<Foo<int>> sf = new SuperFoo<Foo<int>>();

but you can't, so the compiler can't know X unless the class declaration also has X, as in the LinkedList example. You should write:

class SuperFoo<S, X> where S : Foo<X>

now you can write:

SuperFoo<Foo<int>, int> sf = new SuperFoo<Foo<int>, int>();

or if you have a class like this:

class DerivedFoo<T> : Foo<T>

you can write:

SuperFoo<DerivedFoo<int>, int> sf = new SuperFoo<DerivedFoo<int>, int>();

and the constraint will be satisfied....

This is from the spec:

"In either case (class or interface constraint), the constraint may involve any of the type parameters of the associated type or method declaration as part of a constructed type, and may involve the type being declared, but the constraint may not be a type parameter alone."

# re: Friday Quiz: Question on .NET 2.0 Generics 8/23/2004 6:33 AM Rodrigo Jordao

And hey, you could try something like this too:

public class SuperFoo<T> {}

public class NewSuperFoo<T> : SuperFoo<Foo<T>> {}

# re: Friday Quiz: Question on .NET 2.0 Generics 8/24/2004 1:12 PM johnny

Just curious, can you give an example of what type of project would you use something like this?

Johnny

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles