public class Tester<T> where T:new() { public Tester() { t = new T();//等同于非泛型版本的new? 例如 object o = new object();? } private T t; } |
Tester<SomeType> t = new Tester<SomeType>(); //此处编译器会检查SomeType是否具有无参构造函数。若没有则会有compile error。 |
public class Tester<T> where T:new() { public Tester() { t = System.Activator.CreateInstance<T>(); } private T t; } |
联系客服