Explanation why IEnumerable is more efficient than a List

前端 未结 7 1807
北荒
北荒 2021-01-30 08:35

I keep hearing that in .net 3.5 you should use IEnumerable over a List, but I can’t find any reference materials or articles that explain why it’s so much more proficient. Does

7条回答
  •  再見小時候
    2021-01-30 09:26

    In .NET 3.5, using IEnumerable allows you to write methods with deferred execution, such as the following:

    public class MyClass
    {
       private List _listOne;
       private List _listTwo;
    public IEnumerable GetItems () { foreach (int n in _listOne) { yield return n; } foreach (int n in _listTwo) { yield return n; } } }

    This allows you to combine the two lists without creating a new List object.

提交回复
热议问题