Jader Dias
Jader Dias

Reputation: 90475

How to specify a generic type should implement another generic type?

Imagine the following method

public void SomeMethod<T>(T param)
    where T: List<T2>
{
}

It doesn't work:

Error 16 The type or namespace name 'T2' could not be found (are you missing a using directive or an assembly reference?)

How do I achieve the what I clearly intended to do?

Upvotes: 2

Views: 124

Answers (2)

James
James

Reputation: 679

As a side answer to the accepted solution, since T is explicitly related to T2, why have T at all?

public void SomeMethod<T2>(List<T2> listParam) 
{
}

Upvotes: 3

JaredPar
JaredPar

Reputation: 754595

In order to do this, you need to specify an additional generic parameter

public void SomeMethod<T1,T2>(T1 param)
  where T1 : List<T2>
{
}

Upvotes: 9

Related Questions