构造函数参数中的泛型类型

时间:2014-12-10 10:15:27

标签: c# generics

我想在构造函数参数中使用泛型类型,如下所示:

public class Myclass
{
    public Myclass<T>(IEnumerable<T> ListeValeurChampPerso, string str, int a)
     {
       foreach (var vr in ListeValeurChampPerso)
       {...
       }
    }
}

但它会给我带来很多错误。我怎样才能做到这一点? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

你必须使类具有通用性,你不能拥有泛型构造函数,因为它无意义,因为你不能在构造函数之外使用T。

这样可以正常工作。

public class Myclass<T>
{
    public Myclass(IEnumerable<T> ListeValeurChampPerso, string str, int a)
     {
       foreach (var vr in ListeValeurChampPerso)
       {...
       }
    }
}
相关问题