public class My<T> where T: new()
        {
            public T Create()
            {
                return new T();
            }
        }

解决方案 »

  1.   

    因为不是所有的类型都有公开的无参构造函数的。你要告诉编译起T可以用new T():
    public class My<T> where T : new()
    {
        ...
      

  2.   

    相当于加了一个限制条件。这样才能保证T有new可以被调用。
      

  3.   

    异常提示已经很明显地告诉你应该怎么做了,少了约束where T : new()
      

  4.   

    Cannot create an instance of the variable type 'T' because it does not have the new() constraint
    =>
     because 'T' does not have the new() constraint
      

  5.   

    new ().....让编译器知道这个T泛型是有无参数的构造函数的,因为你new T()了
      

  6.   

    在类的声明后面价格约束,where T:new()
    因为方法create里需要new T 所以,T必须要有个无参数的构造函数。
      

  7.   


    class List<T> : where T : new()
    {
    public T gcnewT {
    get{ return new T(); }
    }
    }