我们可以使用通用列表而不是对象数组 C#

发布于 2024-10-31 03:59:40 字数 446 浏览 2 评论 0原文

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++;
  }
}

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

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++;
  }
}

Here I don't know the array length. Need it dynamic depending on the total conditions are true. Is there any efficient way of doing the same using Generic List? If so, how to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坐在坟头思考人生 2024-11-07 03:59:40

您的编码风格是合理且常见的,但请注意它的重要性。你说的是“绕过这个循环,改变这个集合,改变这个变量”,构建一个能做你想要的事情的机器。当有选择时,我更喜欢以声明式风格进行编码,并让编译器为我构建机器。我倾向于这样编写你的程序:

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();

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

Your coding style is reasonable and common, but notice how imperative it is. You're saying "go around this loop, mutate this collection, change this variable", building the machine that does what you want. When given the choice I prefer to code in a declarative style, and let the compiler build the machine for me. I would be inclined to write your program like this:

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();

Let the compiler worry about the loops and variables and whatnot; you can concentrate on the semantics rather than the mechanisms.

明月松间行 2024-11-07 03:59:40

这是非常基本的东西:

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);
    }
}

This is really basic stuff:

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);
    }
}
橪书 2024-11-07 03:59:40
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]));
  }
}
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]));
  }
}
心头的小情儿 2024-11-07 03:59:40

只需将

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++;
  }

Just replace

Student[] students = new Student[10];

with

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

and the loop with:

  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++;
  }
祁梦 2024-11-07 03:59:40

是的,通用的 List 在这里会很好用。

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);
  }
}

Yes a generic List<Student> will work great here.

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);
  }
}
假装爱人 2024-11-07 03:59:40

如果您想要类似的语法:

ArrayList

If you want a similar syntax :

ArrayList

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文