日本一本亚洲最大|日本午夜免费啪视频在|国产自产在线视频一区|亚洲福利精品视频

    <object id="4ihfc"></object>
      
      
    1. <object id="4ihfc"></object>
    2. 我要投稿 投訴建議

      C#foreach與yield語句的介紹

      時間:2020-12-24 16:32:24 句子 我要投稿

      C#foreach與yield語句的介紹

        1. foreach語句

        C#編譯器會把foreach語句轉(zhuǎn)換為IEnumerable接口的方法和屬性。

        復制代碼 代碼如下:

        foreach (Person p in persons)

        {

        Console.WriteLine(p);

        }

        foreach語句會解析為下面的代碼段。

        調(diào)用GetEnumerator()方法,獲得數(shù)組的.一個枚舉

        在while循環(huán)中,只要MoveNext()返回true,就一直循環(huán)下去

        用Current屬性訪問數(shù)組中的元素

        復制代碼 代碼如下:

        IEnumerator enumerator = persons. GetEnumerator();

        while (enumerator.MoveNext())

        {

        Person p = (Person) enumerator.Current;

        Console.WriteLine(p);

        }

        2. yield語句

        yield語句的兩種形式:

        復制代碼 代碼如下:

        yield return;

        yield break;

        使用一個yield return語句返回集合的一個元素

        包含yield語句的方法或?qū)傩允堑。迭代器必須滿足以下要求

        a. 返回類型必須是IEnumerable、IEnumerable、IEnumerator或 IEnumerator。

        b. 它不能有任何ref或out參數(shù)

        yield return語句不能位于try-catch快。yield return語句可以位于try-finally的try塊

        復制代碼 代碼如下:

        try

        {

        // ERROR: Cannot yield a value in the boday of a try block with a catch clause

        yield return "test";

        }

        catch

        { }

        try

        {

        //

        yield return "test again";

        }

        finally

        { }

        try

        { }

        finally

        {

        // ERROR: Cannot yield in the body of a finally clause

        yield return "";

        }

        yield break語句可以位于try塊或catch塊,但是不能位于finally塊

        下面的例子是用yield return語句實現(xiàn)一個簡單集合的代碼,以及用foreach語句迭代集合

        復制代碼 代碼如下:

        using System;

        using System.Collections.Generic;

        namespace ConsoleApplication6

        {

        class Program

        {

        static void Main(string[] args)

        {

        HelloCollection helloCollection = new HelloCollection();

        foreach (string s in helloCollection)

        {

        Console.WriteLine(s);

        Console.ReadLine();

        }

        }

        }

        public class HelloCollection

        {

        public IEnumeratorGetEnumerator()

        {

        // yield return語句返回集合的一個元素,并移動到下一個元素上;yield break可以停止迭代

        yield return "Hello";

        yield return "World";

        }

        }

        }

        使用yield return語句實現(xiàn)以不同方式迭代集合的類:

        復制代碼 代碼如下:

        using System;

        using System.Collections.Generic;

        namespace ConsoleApplication8

        {

        class Program

        {

        static void Main(string[] args)

        {

        MusicTitles titles = new MusicTitles();

        foreach (string title in titles)

        {

        Console.WriteLine(title);

        }

        Console.WriteLine();

        foreach (string title in titles.Reverse())

        {

        Console.WriteLine(title);

        }

        Console.WriteLine();

        foreach (string title in titles.Subset(2, 2))

        {

        Console.WriteLine(title);

        Console.ReadLine();

        }

        }

        }

        public class MusicTitles

        {

        string[] names = { "a", "b", "c", "d" };

        public IEnumeratorGetEnumerator()

        {

        for (int i = 0; i < 4; i++)

        {

        yield return names[i];

        }

        }

        public IEnumerableReverse()

        {

        for (int i = 3; i >= 0; i--)

        {

        yield return names[i];

        }

        }

        public IEnumerableSubset(int index, int length)

        {

        for (int i = index; i < index + length; i++)

        {

        yield return names[i];

        }

        }

        }

        }

        輸出:

      【C#foreach與yield語句的介紹】相關(guān)文章:

      介紹自己榮譽的英語句子06-07

      與合同有關(guān)的英語句子06-16

      職場中介紹他人的英語句子06-09

      職場中性格介紹的英語句子06-06

      與上司溝通的技巧介紹07-17

      介紹元旦的由來與習俗01-11

      能力介紹英語口語句子06-29

      外貿(mào)英語中最常用的語句與詞匯04-22

      職場中常用英語句子與短語06-08

      元旦的由來與新年習俗介紹10-15