开发者

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

开发者 https://www.devze.com 2022-12-15 07:40 出处:网络
Imagine the following method public void SomeMethod<T>(T param) where T: List<T2> { } It doe开发者_StackOverflowsn\'t work:

Imagine the following method

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

It doe开发者_StackOverflowsn'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?


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

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


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) 
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消