[英]Allowing any generic type in method parameter
我想制作一个将任何泛型作为参数的方法,例如,我有以下方法将采用 MyGenericClass<string> 但我想制作它以便我也可以将它传递给 MyGenericClass <int> 或 MyGenericClass<anything else>。 我该怎么做?
private void foo(MyGenericClass<string> tag)
{
}
你可以这样做:
private void foo<T>(MyGenericClass<T> tag)
{
}
如果您的泛型类使用特定类型,您也应该修改方法:
class MyGenericClass<T> where T : struct
{
}
方法:
private static void foo<T>(MyGenericClass<T> tag) where T : struct
{
}
有关更多详细信息,您可以阅读: https : //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-classes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.