class A<T>
{
  //返回一个泛型
   public static T returnOne()
   {
     T myObject;
     myobject=(T)....;
     return myObject;
   }
   //返回一个泛型组??
   public 是他提出 T[]  returnArrey()
   {
    T[] myObject;
   
if (list.count!=0)
{  
    for (int i = 0; i < list.Count; i++)
          {
    
              myObject[i] = (T)list[i];
          }
          return myObject;
       //提示错误:对象未实例化。
   }
}
}
如何返回一个泛型组呀?拜谢。

解决方案 »

  1.   

    打错, public 是他提出 T[]  returnArrey()
    --------
    是 public static T[] returnArrey()
    不好意思。
      

  2.   

    在System.Collections.Generic命名空间下,貌似有一个List<T>泛型数组
      

  3.   

    T[] myObject = new T[list.Count];
      

  4.   

    可以的, 加约束 class A<T>  where T : new
    {
    }
      

  5.   

    T[] myObject = new T[list.Count];
    这个可以!!我都没去试。
    但这个为什么不可以?
    T myObject=new T();
    两者区别在哪呢??
      

  6.   

    public list<string> getString()
    {
      list<string> l=new list<string>();
      l.add("hello");
    return l;
    }
      

  7.   

    区别在于如果T是值类型(比如 int)时,是无法被 new 出来的,所以必须加上约束
    我上面写错了,应该是
    class A<T>  where T : new()
    {
    }