vb.net 返回 IList 与 List 或 IEnumerable 与 List<Class> 之间有什么区别。我想知道返回哪个更好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26907986/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:28:10  来源:igfitidea点击:

What is the difference between returning IList vs List, or IEnumerable vs List<Class>. I want to know which is better to return

c#asp.net.netasp.net-mvcvb.net

提问by virender

What is the difference between returning IList vs List, or IEnumerable vs List.

返回 IList 与 List 或 IEnumerable 与 List 之间有什么区别。

I want to know which is better to return.

我想知道返回哪个更好。

When we need to use one, what effect will it have on performance?

当我们需要使用一个时,它对性能有什么影响?

回答by ialekseev

There is no such a type that is alwaysbetter to return. It's a decision you should make based on your design/performance/etc goals.

没有这样的类型总是更好地返回。这是您应该根据您的设计/性能/等目标做出的决定。

IEnumerable<T>is nice to use when you want to represent sequence of items, that you can iterate over, but you don't want to allow modifications(Add, Delete etc).

IEnumerable<T>当您想要表示可以迭代的项目序列,但您不想允许修改(添加、删除等)时,使用它是很好的。

IList<T>gives you everything you could get using IEnumerable<T>, plus operations that give you more control over a collection: Add, Delete, Count, Index access etc.

IList<T>为您提供可以使用的所有内容IEnumerable<T>,以及使您可以更好地控制集合的操作:添加、删除、计数、索引访问等。

List<T>is a concrete implementation of IList<T>. I would say that almost always it's better to expose IList<T>interface from your methods rather that List<T>implementation. And it's not just about lists - it's a basic design principle to prefer interfaces over concrete implementations.

List<T>是 的具体实现IList<T>。我会说几乎总是IList<T>从你的方法而不是List<T>实现中公开接口更好。这不仅仅是关于列表——它是一个基本的设计原则,即优先于具体实现的接口。

Ok, now about non-generic versions IEnumerable, IList, List: They actually came from very early versions of .NET framework, and life is much better using generic equivalents.

好的,现在关于非泛型版本IEnumerable, IList, List:它们实际上来自 .NET 框架的早期版本,使用泛型等价物的生活会好得多。

And few words about performance: IEnumerable<T>(with IEnumerator<T>) is actually an iterator which allows you to defer some computations until later. It means that there is no need to allocate memory right away for storing amounts of data(of course, it's not the case when you have, say, array behind iterator). You can compute data gradually as needed. But it means that these computations might be performed over and over again(say, with every foreachloop). On the other hand, with List you have fixed data in memory, with cheap Index and Count operations. As you see, it's all about compromise.

关于性能的几句话: IEnumerable<T>(with IEnumerator<T>) 实际上是一个迭代器,它允许您将一些计算推迟到以后。这意味着不需要立即分配内存来存储大量数据(当然,当您拥有迭代器后面的数组时,情况并非如此)。您可以根据需要逐步计算数据。但这意味着这些计算可能会一遍又一遍地执行(例如,每次foreach循环)。另一方面,使用 List 您可以在内存中固定数据,并使用廉价的 Index 和 Count 操作。如您所见,一切都与妥协有关。

回答by Mark Shevchenko

Using concrete classes in parameters and results of methods makes a strong dependency, while using interfaces don't. What it mean?

在方法的参数和结果中使用具体类会产生很强的依赖性,而使用接口则不会。什么意思?

If in the future you'll change the implementation of your class, and will use SynchroinizedCollection, LinkedList, or something other instead of List, then you have to change your methods signature, exactly the type of return value.

如果将来您要更改类的实现,并且将使用SynchroinizedCollectionLinkedList或其他东西代替List,那么您必须更改方法签名,即返回值的类型。

After that you have to not only rebuild assemblies that used this class, but may have to rewrite them.

之后,您不仅必须重建使用此类的程序集,而且可能还必须重写它们。

However, if you're using one of IEnumerable, IReadonlyCollection, ICollection, IListinterfaces, you'll not have to rewrite and recompile client assemblies. Thus, interfaces always preferred classes in parameters and results. (But remember, we're talking about dependencies between different assemblies. With the same assembly this rule is not so important.)

但是,如果您使用IEnumerable, IReadonlyCollection, ICollection,IList接口之一,则不必重写和重新编译客户端程序集。因此,接口总是首选参数和结果中的类。(但请记住,我们讨论的是不同程序集之间的依赖关系。对于相同的程序集,这条规则并不那么重要。)

The question is, what interface to use? It depends on requirements of client classes (use cases). F.e. if you're processing elements one by one, use IEnumerable<T>, and if you need a count of elements, use IReadonlyCollection<T>. Both of these interfaces are co-variance that is convenient for a type-casting.

问题是,使用什么接口?这取决于客户端类(用例)的要求。Fe 如果您要一个一个地处理元素,请使用IEnumerable<T>,如果您需要元素计数,请使用IReadonlyCollection<T>。这两个接口都是协变的,便于类型转换。

If you need write abilities (Add, Remove, Clear) or non co-variance read only abilities (Contains), use ICollection<T>. Finally, if you need a random indexed access, use IList<T>.

如果您需要写入能力 ( Add, Remove, Clear) 或非协方差只读能力 ( Contains),请使用ICollection<T>. 最后,如果您需要随机索引访问,请使用IList<T>.

As for performance, the invocation of interface's method a bit slower, but it's insignificant difference. You shouldn't care about this.

至于性能,调用接口的方法会慢一些,但差别不大。你不应该关心这个。