# [.net] A few generics questions.

This topic is 4407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have two unanswered questions about generics. I'm led to believe that the features are not implemented in any way, but here goes. 1. Is there a way to do something that would achieve similar functionnality to this (made up syntax):
  class Foo<T1, T2, ..., Tn, W<T2>> {}

...

//later
Foo<R1, R2, ..., Rn, W> bar;

and have it that W must be a Generic type which can take R2 as a type parameter ? Specifically, say I want to pass a key type, a value type and an arbitrary collection implementing IDictionnary. It would look something like:
  class Foo<KeyType, ValueType, AssociativeCollection<KeyType, ValueType>
where AssociativeCollection: IDictionnary<KeyType, ValueType>
{}

...

//later
Foo<int, float, MyDictionnaryImplementation> bar;

and avoid crazy long lines with redundant type parameters ? 2. Is there a way to specify a default type argument so that it can be ommited ? [Edited by - jfclavette on July 28, 2006 10:34:25 PM]

##### Share on other sites
I think:

1. Eh, not really. You could probably use a generic method to fill in the types and return the full item, but it's probably simpiler just to use the constraint you already posted and double enter the types.

2. Eh, not really. You could do:
public class Foo<K,V,D> where D:IDictionary<K,V>{...}public class Foo:Foo<int,int,Dictionary<int,int>>{...}

but that just creates two different class types, and would require special care in constructing the generic foo so that the default foo exposes everything similarly.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004755

×