yield return 關鍵字的詳解

http://2sws.blog.163.com/blog/static/179102492009843454582/

 

/// <summary>
/// yield return 關鍵字的詳解
/// 下面的例子可以進行正常的迭帶運算,想要理解運行的機制的
/// 話,我們可以使用F11鍵進行單步運行,就可以看明白了。
/// </summary>

public class Persons : System.Collections.IEnumerable
{
    #region IEnumerable 成員

    public System.Collections.IEnumerator GetEnumerator()
    {
        yield return "1";
        yield return "2";
        yield return "3";
        yield return "4";
        yield return "5";
        yield return "6";
    }

    #endregion
}

class program
{
    static void Main()
    {
        Persons arrPersons = new Persons();
        foreach (string s in arrPersons)
        {
            System.Console.WriteLine(s);
        }

        System.Console.ReadLine();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章