2
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?

0

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Jon, I implemented your solution but then I saw Eric's and changed my code that way. But still your solution was little simpler :)
1
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]));
  }
}

Comments

1

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

Comments

1

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

Comments

0

If you want a similar syntax :

ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.