Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a generic class that takes as its generic type a generic class?

Tags:

c#

generics

Basically, I want to write a wrapper for all ICollection<> types. Lets call it DelayedAddCollection. It should take any ICollection as its .

Furthermore, I need access to that ICollection type's generic type as the Add method needs to restrict its parameter to that type.

The syntax I would imagine would look something like this...

public DelayedAddConnection<T>: where T:ICollection<U> {
   ....

   public void Add(U element){
     ...
   }
}

What is the real correct syntax to do this?

like image 262
user430788 Avatar asked Sep 09 '13 19:09

user430788


1 Answers

You need to add another generic type parameter:

public class DelayedAddConnection<T, U> where T : ICollection<U>
{

}
like image 174
Lee Avatar answered Oct 21 '22 09:10

Lee