为什么使用"IList"而不是"List"?

28 浏览
0 Comments

为什么使用"IList"而不是"List"?

假设我有这个Student类:\n

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

\n我喜欢使用List来创建Student的集合:\n

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

\n但是我听说通常最好使用IList:\n

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

\n我不明白为什么人们推荐使用IList而不是List!\n这是出于性能方面的考虑吗?这是可选的,取决于个人喜好吗?还是有其他原因?

0