什么是返回的ICollection&LT的真正优势; T> ;? T>而不是一个名单,其中 [英] What is the real advantage of returning ICollection<T> instead of a List<T>?

查看:188
本文介绍了什么是返回的ICollection&LT的真正优势; T> ;? T>而不是一个名单,其中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几个博客中提到,对于公共API,我们应该始终返回,而不是名单的ICollection(或IEnumerable的)。什么是返回一个列表的ICollection,而不是真正的优势在哪里?

I've read a couple of blog post mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List?

谢谢!

复制:<一href="http://stackoverflow.com/questions/398903/what-is-the-difference-between-list-of-t-and-collectionof-t">What是列表(的T)和集合(T)的?

推荐答案

这是枚举器仅返回一次一个实体,你遍历它。这是因为它使用了收益率回归。集合,在另一方面,返回整个列表,要求该列表被完全存储在存储器

An enumerator only returns one entity at a time as you iterate over it. This is because it uses a yield return. A collection, on the other hand, returns the entire list, requiring that the list be stored completely in memory.

简短的回答是,统计员更轻,更高效的。

The short answer is that enumerators are lighter and more efficient.

这篇关于什么是返回的ICollection&LT的真正优势; T&GT; ;? T&GT;而不是一个名单,其中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆