I have a generic class - Class1<T>
, and a regular class - Class2
.
I want to create a generic method in Class2 that gets Class1 as a parameter. I tried to do:
void DoSomething<T>(Class1<T> data) {
...
}
But I get an error:
Error 25: The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method '
Class1<T>
'.
How can I do it?
听起来您在Class<T>
上有where T : class
限制,所以您只需要修改方法签名即可:
void DoSomething<T>(Class1<T> data) where T : class
I think you need to define the Class1<T>
as below:
public class Class1<T> where T : class // here `T` is regarded as a reference type
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.