使用List< T>的好处是什么?通过IEnumerable< T> ;? [英] What is the benefit to using List<T> over IEnumerable<T>?
问题描述
我一直使用泛型列表。但我偶尔也会听到有关IEnumerables的信息,而且我真的不知道他们是什么,为什么我应该使用它们。所以,在网络上有东西的风险永远更多地宣布我的无知,我虚心地发表这个问题。
解决方案
List< T>
implements IEnumerable< T>
...基本上 IEnumerable< T> / code>只是一系列的项目。
列表< T>
是一个可变的集合 - 您可以添加对它进行删除,排序等操作。它本身更加灵活,但是 IEnumerable< T>
允许您使用相同的代码来处理任何实现(数组,链表,列表,使用 yield
语句等方法返回的迭代器)。
or the other way around?
I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at the risk of having something on the net forever more proclaiming my ignorance, I humbly post this question.
解决方案 Well, List<T>
implements IEnumerable<T>
... basically IEnumerable<T>
is just a sequence of items. You can read through it, and that's all.
List<T>
is a mutable collection - you can add to it, remove from it, sort it etc. It's more flexible in itself, but IEnumerable<T>
allows you to use the same code to work with any implementation (arrays, linked lists, lists, iterators returned from methods using yield
statements etc).
这篇关于使用List< T>的好处是什么?通过IEnumerable< T> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!