【问题标题】:initialize value to a public variable of type list of a class using the object initializer使用对象初始化器将值初始化为类的列表类型的公共变量
【发布时间】:2018-08-15 05:11:05
【问题描述】:

我有一个泛型类,它只包含一个列表类型的数据成员。现在,我想使用我的 main 方法中该泛型类的对象初始化程序向该列表添加值。

这是我的通用类

class GenericStore<T>
{
    public List<T> allData = new List<T>();
}

这里是我的入口点

class Program
{
    static void Main(string[] args)
    {
        GenericStore<Student> studentData = new GenericStore<Student>()
        {
           // I Have Write This Which Gives me Error

            /*allData =  new Student(new Guid(), "Subhashis Pal"),
            allData =  new Student(new Guid(), "x"),
            allData =  new Student(new Guid(), "Y"),
            allData = new Student(new Guid(), "Z")*/
        };

    }
}

这是我的学生课

class Student
{
    private Guid id;
    private string name;
    public Student(Guid id, string name)
    {
        this.id = id;
        this.name = name;
    }
}

【问题讨论】:

  • 你为什么不向你的商店添加一个构造函数,它接受一个类型为 T 的参数并将一个学生传递给它?
  • 哪个错误?也请提及。
  • 您确实应该编辑您的帖子并添加错误消息。实际上它信息量很大:Cannot implicitly convert type 'Student' to 'System.Collections.Generic.List&lt;Student&gt;'。这意味着您的类型不匹配。如果您开始研究compiler error code,您可以自己解决这个问题。 :)
  • 实际上研究了你问题的整个标题给了我this as the first result。发布的答案在那里。唯一的区别是他们使用的是猫;)而不是学生
  • 实际上我很困惑,因为 allData 是一个类的成员,我正在使用对象初始化程序。我理解 Cat 的例子,但我应该弄清楚。谢谢。 :)

标签: c# generics object-initializers


【解决方案1】:

allData 是 List&lt;T&gt;,而您每次都尝试将单个对象 Student 分配给它。

使用 object intializer 填充 allData,如下所示:

GenericStore<Student> studentData = new GenericStore<Student>
{
    allData = new List<Student>
    {
        new Student(new Guid(), "Subhashis Pal"),
        new Student(new Guid(), "x"),
        new Student(new Guid(), "Y"),
        new Student(new Guid(), "Z"),
    }
};

【讨论】:

    【解决方案2】:

    我认为您对对象初始化程序的工作方式感到困惑。这个:

    GenericStore<Student> studentData = new GenericStore<Student>()
    {
        allData =  new Student(new Guid(), "Subhashis Pal"),
        allData =  new Student(new Guid(), "x"),
        allData =  new Student(new Guid(), "Y"),
        allData = new Student(new Guid(), "Z")
    };
    

    不正确,因为不能多次分配一个字段,并且Student 与List&lt;Student&gt; 不兼容。正确的做法是

    GenericStore<Student> studentData = new GenericStore<Student>()
    {
        allData = new List<Student>() 
        {
            // and you create your student objects *here*
        }
    };
    

    您需要将List&lt;Student&gt; 正确分配给allData。然后,您可以使用 list 初始化程序用学生对象初始化列表。

    【讨论】:

      【解决方案3】:

      它会给你一个错误,因为 allData 字段的类型是 List&lt;Student&gt; 在 GenericStore&lt;Student&gt; 的情况下,所以为了在对象初始化器中播种该字段,您需要实例化 List&lt;Student&gt; 集合并使用它的对象初始化器添加Student对象

      GenericStore<Student> store = new GenericStore<Student>
          {
              allData = new List<Student> 
              {
                  new Student(new Guid(), "Subhashis Pal"),
                  new Student(new Guid(), "x"),
                  new Student(new Guid(), "Y"),
                  new Student(new Guid(), "Z")
              }
          }
      

      【讨论】:

      • 好的,非常感谢。 :)
      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2013-06-27
      • 2016-11-03
      相关资源
      最近更新 更多