2

Suppose I have this Student class:

public class Student
{
    public int StudentID { set; get; }
    public string StudentName { set; get; }
    public int Age { set; get; }
}

I like to create collections of Students using List:

List<Student> studentList2 = new List<Student>() { 
   new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
   new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } 
};

But I've heard that it's usually better to use IList:

IList<Student> studentList1 = new List<Student>() { 
    new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
    new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 }
};

I don't understand why people recommend using IList<Student> instead of List<Student>!

Is it some kind of performance concern? Is it optional and down to personal preference? Or is there another reason?

Kevin
  • 14,655
  • 24
  • 74
  • 124

0 Answers0