[编程基础][C#]IEnumerable基础学习

 

namespace System.Collections
{
    //
    // 摘要:
    //     Exposes an enumerator, which supports a simple iteration over a non-generic collection.
    public interface IEnumerable
    {
        //
        // 摘要:
        //     Returns an enumerator that iterates through a collection.
        //
        // 返回结果:
        //     An System.Collections.IEnumerator object that can be used to iterate through
        //     the collection.
        IEnumerator GetEnumerator();
    }
}

 

 

一、yield使用案例

public static void Main()
{
    foreach (var i in Fibonacci().Take(20))
    {
        Console.WriteLine(i);
    }

    Console.WriteLine("Hello World!");
}

private static IEnumerable<int> Fibonacci()
{
    int current = 1, next = 1;

    while (true)
    {
        yield return current;
        next = current + (current = next);
    }
}

 

posted @ 2021-02-27 16:48  mwy9  阅读(56)  评论(0编辑  收藏  举报