简体   繁体   English

接口列表与实现列表

[英]Interface list vs. implemented list

In my understanding we use interfaces to let the user also use custom list(which need to implement the IList for example).在我的理解中,我们使用接口让用户也使用自定义列表(例如需要实现 IList)。

In many code exercises I see something like this.在许多代码练习中,我看到了这样的事情。

IList<string> interfaceList = new List<string>();

My question is.我的问题是。 Isn't the following list as capable as the first one since both get implemented by List.下面的列表不是和第一个列表一样强大,因为它们都是由 List 实现的。 If yes why use IList when not using custom Lists?如果是,为什么不使用自定义列表时使用 IList?

List<string> normalList = new List<string>();

Isn't the following list as capable as the first one下面的列表是不是和第一个一样强大

It is as there is no difference between the LISTS - both are crated with the same statement.这是因为 LISTS 之间没有区别 - 两者都用相同的语句装箱。

The only difference is in the type of VARIABLE HOLDING THE REFERENCE - not the list itself (which is in both cases a List.唯一的区别在于持有引用的变量的类型——而不是列表本身(在这两种情况下都是一个列表。

It is a matter of style - using IList allows you to easier replace the list with another type implementing IList.这是一个风格问题 - 使用 IList 可以让您更容易地用另一种实现 IList 的类型替换列表。

That being said, depending on USAGE you may find out that List is not JUST an IList but also defines OTHER methods.话虽如此,根据 USAGE,您可能会发现 List 不仅仅是一个 IList,还定义了其他方法。 Hence the use of IList may actually be impossible.因此,使用 IList 实际上可能是不可能的。

Also note that practically for the vast majority of cases it makes no difference - MOST variables will be small and local in use.还要注意,实际上对于绝大多数情况,它没有区别 - 大多数变量将很小并且在使用中是局部的。 As such, arguments like "dynamically injecting another type" etc. are irrelevant - there simply is not enough visibility to even mock the created type.因此,像“动态注入另一种类型”等参数是无关紧要的 - 根本没有足够的可见性来模拟创建的类型。 Simply as most variables are local.就像大多数变量都是局部的一样。

Whilst IList<> may implement all the functionality that List<> does, I would generally recommend against IList<> at all, and suggest you use either IEnumerable<> or ICollection<> instead.虽然IList<>可以实现List<>所做的所有功能,但我通常建议不要使用IList<> ,并建议您改用IEnumerable<>ICollection<>

IEnumerable<> for collections that don't require modification, and ICollection<> for collections that you are allowed to modify. IEnumerable<>用于不需要修改的集合, ICollection<>用于您可以修改的集合。

Doing this allows you to swap out the implementation for a more efficient one, just at the point of creation, rather than having to modify every function that references the collection.这样做可以让您在创建时将实现换成更高效的实现,而不必修改引用集合的每个函数。

Imagine you decide to change a List<> to HashSet<> .假设您决定将List<>更改为HashSet<> If your code uses IList<> then every reference will have to change, whereas, using IEnumerable<> would mean you only make 1 change.如果您的代码使用IList<>则每个引用都必须更改,而使用IEnumerable<>意味着您只需进行 1 次更改。

To answer shortly and concisely your excellent question, the answer is No .要简短而简洁地回答您的好问题,答案是否定的 But it is important to understand the reason for it, because this understanding opens the door for abstract thinking.但理解其原因很重要,因为这种理解为抽象思维打开了大门。 It all boils down to the abstract.这一切都归结为抽象。

In programming you want to solve as many types of issues as possible with a code as short as possible.在编程中,您希望用尽可能短的代码解决尽可能多类型的问题。

As a result, you need to have data structures that are specific-enough, but as far as that need is met, you should strongly prefer to generalize the solution AMAP.因此,您需要具有足够具体的数据结构,但就满足该需求而言,您应该强烈倾向于概括解决方案 AMAP。

All List objects are also IList , but not all objects that are IList are also List .所有List对象也是IList ,但并非所有IList对象也是List

So, if your code assumes that your object is a List , then it necessarily supports far less cases than if you would be more agnostic and would say that it is an IList .因此,如果您的代码假定您的对象是一个List ,那么它支持的情况必然比您更加不可知并会说它是一个IList情况少得多。

If IList properly describes all the methods you might possibly want to call, then it is specific-enough to solve your problem, so it is good.如果IList正确地描述了您可能想要调用的所有方法,那么它就足够具体了,足以解决您的问题,所以它很好。 Now, let's suppose that you need to implement a method that searches for an item insidea list.现在,让我们假设您需要实现一个在列表中搜索项目的方法。 If you implement it so as the parameter it expects is an instance of List , then your method will not work for any objects that are IList , but not List .如果您实现它以便它期望的参数是List的实例,那么您的方法将不适用于IList任何对象,但不适用于List So, a decision to declare something as a List instead of IList should always be some strong argument, like, for some well-understood reason, you do not want to support any other IList , or something that List is capable of but IList is not capable of is needed by the source-code section in question.因此,将某些内容声明为List而不是IList应该始终是一些强有力的论据,例如,出于一些众所周知的原因,您不想支持任何其他IList ,或者List能够支持但IList不支持的内容有问题的源代码部分需要有能力。

IList 还允许您在单元测试中模拟它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM