为什么使用"IList"而不是"List"?
- 论坛
- 为什么使用"IList"而不是"List"?
28 浏览
为什么使用"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
ListstudentList2 = new List () { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } };
\n但是我听说通常最好使用IList
:\n
IListstudentList1 = new List () { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } };
\n我不明白为什么人们推荐使用IList
而不是List
!\n这是出于性能方面的考虑吗?这是可选的,取决于个人喜好吗?还是有其他原因?