[英]C++ templates versus C# generic method with various parameters
在C ++中,这可行。
template<class T> void func(T t)
{ t.method(); }
如何在C#中实现呢? 这是我尝试过的方法,但是不起作用。
void func<T>(T t)
{ t.method(); }
还是不可能在C#中做到这一点?
以下应该工作
public static class MyClass
{
public static void MyMethod<T>(T t) where T : class
{
t.ToString(); // Note: for this to work we need to KNOW the type which defines `method`.
}
}
以及这个:
public class MyBase
{
void Method();
}
public static class MyClassForBase
{
public static void MyMethod<T>(T t) where T : MyBase
{
t.Method(); // Note: anything from MyBase is now available
}
}
最后但并非最不重要的是,您可以像这样使用后期绑定
public static class MyClassDynamic
{
public static void MyMethod(dynamic t)
{
t.Method(); // Note: if t doesn't have a `Method` defined, the code will crush-n-burn at runtime
}
}
在采用通用参数的方法中,您只能在编译器知道将要在该参数上使用的参数上调用方法。
在您的示例中:
void func<T>(T t)
{
t.method();
}
编译器不知道方法method
存在,因此这将无法工作。
您可以通过使用约束告诉编译器哪些方法可用。 例如,如果方法是在接口上定义的,则可以使用where
子句将其正确约束,如下所示:
pubic interface IClassWithMethod
{
void method();
}
void func<T>(T t) where T : IClassWithMethod
{
t.method();
}
C#中的泛型与C ++中的模板不同。 C ++模板本质上是marcos,可在编译时根据其使用的类型来应用于代码。 因此,如果您尝试在没有.method()
方法的类上使用模板,则该模板将在编译时失败。
C#泛型,而不是要求任何类型的T
可能是必须定义一个通用的方法使用的所有方法和属性。 因此,如果您有定义.method()
方法的基本类型或接口, .method()
将泛型方法限制为该类型:
void func<T>(T t) where T:IHaveMethod
{ t.method(); }
只要IHaveMethod
接口定义了method()
,泛型函数就会编译。 请注意,在您的特定示例中,通用方法没有用,因为您只能在接口类型上调用method
。
当您希望返回类型基于输入类型时,泛型更为有用:
T func<T>(T t) where T:IHaveMethod
{ t.method(); return t;}
现在,不仅会调用method
,而且返回类型也正是泛型参数的类型,而不仅仅是IHaveMethod
,这可能会限制您可以在不进行强制转换的情况下对返回值进行处理。
最重要的是,不要像对待C ++模板那样考虑泛型。 这是非常根本的区别,这将使您更难了解何时/应在何处使用泛型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.