【问题标题】:Can we use generic list instead of array of objects C#我们可以使用通用列表而不是对象数组C#
【发布时间】:2011-04-05 16:02:19
【问题描述】:
class Student
{
  public string ID { get; set; }
  public string Name { get; set; }
}

Student[] students = new Student[10];
int j = 0;

for(int i=0; i < 100; i++)
{
  if (some condition)
  {
    student[j].ID = anotherIDlist[i]; //some value from another list;
    student[j].Name = anotherNamelist[i]; //some value from another list;
    j++;
  }
}

这里我不知道数组长度。需要它动态取决于总条件是否正确。有没有使用通用列表做同样的有效方法?如果有,该怎么做?

【问题讨论】:

    标签: c# arrays generics generic-list


    【解决方案1】:

    您的编码风格合理且普遍,但请注意它的必要性。你说的是“绕过这个循环,改变这个集合,改变这个变量”,构建你想要的机器。当给出选择时,我更喜欢以 declarative 风格进行编码,并让编译器为我构建机器。我倾向于这样写你的程序:

    var query = from i in Enumerable.Range(0, 100)
                where some_condition
                select new Student() { Id = ids[i], Name = names[i] };
    var students = query.ToList();
    

    让编译器担心循环和变量等等;您可以专注于语义而不是机制。

    【讨论】:

      【解决方案2】:

      这是非常基本的东西:

      var students = new List<Student>();
      
      for(int i=0; i < 100; i++)
      {
          if (some condition)
          {
              // You can produce the student to add any way you like, e.g.:
              someStudent = new Student { ID = anotherIDlist[i], Name = anotherNamelist[i] };
              students.Add(someStudent);
          }
      }
      

      【讨论】:

      • 乔恩,我实现了您的解决方案,但后来我看到了 Eric 并以这种方式更改了我的代码。但是您的解决方案仍然简单一点:)
      【解决方案3】:
      List<Students> students = new List<Students>;
      
      for(int i=0; i < 100; i++)
      {
        if (some condition)
        {
          students.Add(new Student { .ID = anotherIDlist[i], .Name = anotherNamelist[i]));
        }
      }
      

      【讨论】:

        【解决方案4】:

        只需替换

        Student[] students = new Student[10];
        

        List<Student> students = new List<Student();
        

        和循环:

          if (some condition)
          {
            Student student = new Student();
            student.ID = anotherIDlist[i]; //some value from another list;
            student.Name = anotherNamelist[i]; //some value from another list;
            students.Add(student);
            j++;
          }
        

        【讨论】:

          【解决方案5】:

          是的,通用的List&lt;Student&gt; 在这里会很好用。

          List<Student> students = new List<Student>();
          for(int i=0; i < 100; i++)
          {
            if (some condition)
            {
              Student s = new Student();
              s.ID = anotherIDlist[i]; //some value from another list;
              s.Name = anotherNamelist[i]; //some value from another list;
              students.Add(s);
            }
          }
          

          【讨论】:

            【解决方案6】:

            如果你想要类似的语法:

            ArrayList

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-09-07
              • 2015-03-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-02-03
              相关资源
              最近更新 更多