C# IList<T> 和 List<T> 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12369570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between IList<T> and List<T>
提问by Coder
Possible Duplicate:
C# - List<T> or IList<T>
可能的重复:
C# - List<T> 或 IList<T>
I have a class
我有一堂课
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
And I need to define a list and what is the difference between defining it in below ways
我需要定义一个列表,以及用以下方式定义它有什么区别
IList<Employee> EmpList ;
Or
List<Employee> EmpList ;
回答by Coder
IList is an interface, List is a class that implements it, the List type explicitly implements the non generic IList interface
IList 是一个接口,List 是一个实现它的类,List 类型显式地实现了非泛型 IList 接口
回答by Clafou
The first version is programming to interfaces and is preferred (assuming you only need to use the methods defined by IList). The second version, with its declaration based on a specific class, is unnecessarily rigid.
第一个版本是编程接口,是首选(假设您只需要使用 IList 定义的方法)。第二个版本,其声明基于特定的类,是不必要的僵化。
回答by Brian Warshaw
I'll leave you to enumerate the differences, perhaps with some nifty reflection, but a List<T>implements several interfaces, and IList<T>is only one of them:
我会让你列举不同之处,也许会有一些漂亮的反射,但是 aList<T>实现了几个接口,并且IList<T>只是其中之一:
[SerializableAttribute]
public class List<T> : IList<T>, ICollection<T>,
IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>,
IEnumerable
回答by pdriegen
IList<T>is an interface implemented by List<T>.
IList<T>是由实现的接口 List<T>.
You cannot create a concrete instance of an interface so:
您无法创建接口的具体实例,因此:
//this will not compile
IList<Employee> EmpList = new IList<Employee>();
//this is what you're really looking for:
List<Employee> EmpList = new List<Employee>();
//but this will also compile:
IList<Employee> EmpList = new List<Employee>();
回答by StuartLC
IList<>is an interface. List<>is a concrete class.
IList<>是一个接口。List<>是一个具体的类。
Any of these will be valid:
这些中的任何一个都将是有效的:
IList<Employee> EmpList = new List<Employee>();
and
和
List<Employee> EmpList = new List<Employee>();
or
或者
var EmpList = new List<Employee>(); // EmpList is List<Employee>
However, you cannot instantiate an interface, i.e. the following will fail:
但是,您不能实例化接口,即以下操作将失败:
IList<Employee> EmpList = new IList<Employee>();
In general, classes and methods which use dependencies (such as collections) should specify the least restrictive interface possible (i.e. the most general one). e.g. if your method just needs to iterate a collection, then an IEnumerable<>will suffice:
通常,使用依赖项(例如集合)的类和方法应该指定限制最少的接口(即最通用的接口)。例如,如果您的方法只需要迭代一个集合,那么 anIEnumerable<>就足够了:
public void IterateEmployees(IEnumerable<Employee> employees)
{
foreach(var employee in employees)
{
// ...
}
}
Whereas if a consumer needs to access the Countproperty (as opposed to having to iterate the collection via Count()), then a ICollection<T>or better, IReadOnlyCollection<T>would be more appropriate, and similarly, IList<T>would only be required when needing random access to the collection via []or to express that new items need to be added or removed from the collection.
而如果消费者需要访问该Count属性(而不是必须通过 迭代集合Count()),那么 aICollection<T>或更好的方法IReadOnlyCollection<T>会更合适,同样,IList<T>只有在需要通过[]或 表示随机访问集合时才需要需要从集合中添加或删除新项目。
回答by antonijn
The difference is that IList is an interface and List is a class. List implements IList, but IList can't be instantiated.
区别在于 IList 是一个接口,而 List 是一个类。List实现了IList,但是IList不能被实例化。
回答by akton
There are two answers here. For storing the actual list, use an List<T>because you need a concrete data structure. However, if you return it from a property or require it as an argument, consider a IList<T>. It is more generic, allowing more types to be passed it for the argument. Similarly, it allows more types to be returned than just the List<T>in case the internal implementation changes. Indeed, you may want to consider an IEnumerable<T>for the return type instead.
这里有两个答案。为了存储实际列表,请使用 anList<T>因为您需要一个具体的数据结构。但是,如果您从属性返回它或要求将其作为参数,请考虑使用IList<T>. 它更通用,允许为参数传递更多类型。同样,它允许返回更多类型,而不仅仅是List<T>在内部实现更改的情况下。实际上,您可能需要考虑使用 anIEnumerable<T>作为返回类型。
回答by Maddy
Listobject allows you to create a list, add things to it, remove it, update it, index into it etc etc. Listis used whenever you just want a generic List where you specify object type in it and that's it.
Listobject 允许你创建一个列表,向它添加东西,删除它,更新它,索引它等等。List只要你想要一个通用列表,你就可以在其中指定对象类型,就是这样。
IListon the other hand is an Interface. (For more on interfaces see MSDN Interfaces). Basically, if you want to create your own type of List, say a list class called SimpleList, then you can use the Interface to give you basic methods and structure to your new class. IListis for when you want to create your own, special sub-class that implements List.
You can see example here
IList另一方面是一个接口。(有关接口的更多信息,请参阅 MSDN 接口)。基本上,如果你想创建你自己的类型List,比如一个名为 SimpleList 的列表类,那么你可以使用接口为你的新类提供基本的方法和结构。IList用于当您想创建自己的特殊子类来实现List.
您可以在此处查看示例
回答by tehdoommarine
There are many types of Lists. Each of them inherits from an IList (which is why it's an interface). Two examples are the List (regular list), and a Paged List (this is a list that has support for paging - it is used commonly in paged search results). A Paged List and a List are both types of ILists, which means that an IList is not necessary a List (it can be a Paged List) or vice-versa.
有很多类型的列表。它们中的每一个都继承自 IList(这就是为什么它是一个接口)。两个示例是列表(常规列表)和分页列表(这是一个支持分页的列表 - 它通常用于分页搜索结果)。Paged List 和 List 都是 IList 的类型,这意味着 IList 不一定是 List(它可以是 Paged List),反之亦然。
See this link on a PagedList. https://github.com/TroyGoode/PagedList#readme
请参阅 PagedList 上的此链接。https://github.com/TroyGoode/PagedList#readme

