溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

yield實(shí)現(xiàn)什么功能

發(fā)布時(shí)間:2020-08-05 17:14:37 來(lái)源:億速云 閱讀:133 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下yield實(shí)現(xiàn)什么功能,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

1.yield實(shí)現(xiàn)的功能

yield return:

先看下面的代碼,通過(guò)yield return實(shí)現(xiàn)了類似用foreach遍歷數(shù)組的功能,說(shuō)明yield return也是用來(lái)實(shí)現(xiàn)迭代器的功能的。

using static System.Console;
using System.Collections.Generic;

class Program
{
  //一個(gè)返回類型為IEnumerable<int>,其中包含三個(gè)yield return
  public static IEnumerable<int> enumerableFuc()
  {
    yield return 1;
    yield return 2;
    yield return 3;
  }

  static void Main(string[] args)
  {
    //通過(guò)foreach循環(huán)迭代此函數(shù)
    foreach(int item in enumerableFuc())
    {
      WriteLine(item);
    }
    ReadKey();
  }
}

輸出結(jié)果:

1
2
3

yield break:

再看下面的代碼,只輸出了1,2,沒(méi)有輸出3,說(shuō)明這個(gè)迭代器被yield break停掉了,所以yield break是用來(lái)終止迭代的。

using static System.Console;
using System.Collections.Generic;
class Program
{
  //一個(gè)返回類型為IEnumerable<int>,其中包含三個(gè)yield return
  public static IEnumerable<int> enumerableFuc()
  {
    yield return 1;
    yield return 2;
    yield break;
    yield return 3;
  }

  static void Main(string[] args)
  {
    //通過(guò)foreach循環(huán)迭代此函數(shù)
    foreach(int item in enumerableFuc())
    {
      WriteLine(item);
    }
    ReadKey();
  }
}

輸出結(jié)果:

1
2

2.只能使用在返回類型必須為 IEnumerable、IEnumerable<T>、IEnumerator 或 IEnumerator<T>的方法、運(yùn)算符、get訪問(wèn)器中。

3.yield關(guān)鍵字的實(shí)現(xiàn)原理

我們用while循環(huán)代替foreach循環(huán),發(fā)現(xiàn)我們雖然沒(méi)有實(shí)現(xiàn)GetEnumerator(),也沒(méi)有實(shí)現(xiàn)對(duì)應(yīng)的IEnumerator的MoveNext(),和Current屬性,但是我們?nèi)匀荒苷J褂眠@些函數(shù)。

class Program
{
  //一個(gè)返回類型為IEnumerable<int>,其中包含三個(gè)yield return
  public static IEnumerable<int> enumerableFuc()
  {
    yield return 1;
    yield return 2;
    yield return 3;
  }

  static void Main(string[] args)
  {
    //用while循環(huán)代替foreach
    IEnumerator<int> enumerator = enumerableFuc().GetEnumerator();
    while (enumerator.MoveNext())
    {
      int current = enumerator.Current;
      WriteLine(current);
    }
    ReadKey();
  }
}

輸出結(jié)果:

1
2
3

至于為什么會(huì)出現(xiàn)這種情況,我們可以用ILSpy對(duì)生成的exe進(jìn)行反編譯來(lái)找到原因。
由于直接反編譯成C#會(huì)變?yōu)樵瓨?/p>

yield實(shí)現(xiàn)什么功能

所以我們選擇反編譯為帶C#注釋的IL代碼,雖然可讀性差點(diǎn),但是可以詳細(xì)的了解其中過(guò)的原理。
先來(lái)看Program翻譯的情況,編譯的時(shí)候自動(dòng)生成了一個(gè)新的類。

yield實(shí)現(xiàn)什么功能

接下來(lái)我們來(lái)仔細(xì)看這些代碼,EnumerableFuc()返回了這個(gè)新的類。

yield實(shí)現(xiàn)什么功能

看這個(gè)代碼自動(dòng)生成的類的實(shí)現(xiàn),發(fā)現(xiàn)它繼承了IEnumerable、IEnumerable<T>、IEnumerator 或 IEnumerator<T>,這時(shí)我們應(yīng)該已經(jīng)能猜到這個(gè)新的類就是我們沒(méi)有實(shí)現(xiàn)對(duì)應(yīng)的IEnumerator的MoveNext(),和Current屬性,但是我們?nèi)匀荒苷J褂眠@些函數(shù)的原因了。

yield實(shí)現(xiàn)什么功能

我們?cè)賮?lái)看一下這個(gè)類具體是如何實(shí)現(xiàn)迭代的呢,我們主要來(lái)看一下MoveNext()函數(shù)

yield實(shí)現(xiàn)什么功能

yield實(shí)現(xiàn)什么功能

每次調(diào)用MoveNext()函數(shù)都會(huì)將state加1,一共進(jìn)行了4次迭代,前三次返回true,最后一次返回false,代表迭代結(jié)束。這四次迭代對(duì)應(yīng)被3個(gè)yield return語(yǔ)句分成4部分的enumberableFuc()中的語(yǔ)句。

用enumberableFuc()來(lái)進(jìn)行迭代的真實(shí)流程就是:

1.運(yùn)行enumberableFuc()函數(shù),獲取代碼自動(dòng)生成的類的實(shí)例。
2.接著調(diào)用GetEnumberator()函數(shù),將獲取的類自己作為迭代器開始迭代。
3.每次運(yùn)行MoveNext(),state增加1,通過(guò)switch語(yǔ)句可以讓每次調(diào)用MoveNext()的時(shí)候執(zhí)行不同部分的代碼。
4。MoveNext()返回false,結(jié)束。

這也能說(shuō)明yield關(guān)鍵字其實(shí)是一種語(yǔ)法糖,最終還是通過(guò)實(shí)現(xiàn)IEnumberable<T>、IEnumberable、IEnumberator<T>和IEnumberator接口實(shí)現(xiàn)的迭代功能。

看完了這篇文章,相信你對(duì)yield實(shí)現(xiàn)什么功能有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI