在C#中,你可以把一个约束在一个通用的方法,如:在C#中有参数约束的泛型构造函数吗?
public class A {
public static void Method<T> (T a) where T : new() {
//...do something...
}
}
如果您指定T
应该有一个不需要参数的构造函数。我不知道是否有添加约束类似的方式
下面的代码无法编译“存在有float[,]
参数的构造函数?”:
public class A {
public static void Method<T> (T a) where T : new(float[,] u) {
//...do something...
}
}
一种解决方法是也有用?
的可能重复[如何约束通用类型必须有一个construtor这需要一定的参数?](http://stackoverflow.com/questions/853703/how-to-constrain-generic-type-to -must-have-a-construtor-that-certain-certain-param) – nawfal 2014-07-16 14:52:10