欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C#中yield關(guān)鍵字的用法

 更新時間:2023年07月25日 10:31:49   作者:橙子家  
yield?關(guān)鍵字的用途是把指令推遲到程序?qū)嶋H需要的時候再執(zhí)行,這個特性允許我們更細(xì)致地控制集合每個元素產(chǎn)生的時機(jī),那么下面就一起來看下怎么用?yield?關(guān)鍵字吧

〇、前言

yield 關(guān)鍵字的用途是把指令推遲到程序?qū)嶋H需要的時候再執(zhí)行,這個特性允許我們更細(xì)致地控制集合每個元素產(chǎn)生的時機(jī)。

對于一些大型集合,加載起來比較耗時,此時最好是先返回一個來讓系統(tǒng)持續(xù)展示目標(biāo)內(nèi)容。類似于在餐館吃飯,肯定是做好一個菜就上桌了,而不會全部的菜都做好一起上。

另外還有一個好處是,可以提高內(nèi)存使用效率。當(dāng)我們有一個方法要返回一個集合時,而作為方法的實(shí)現(xiàn)者我們并不清楚方法調(diào)用者具體在什么時候要使用該集合數(shù)據(jù)。如果我們不使用 yield 關(guān)鍵字,則意味著需要把集合數(shù)據(jù)裝載到內(nèi)存中等待被使用,這可能導(dǎo)致數(shù)據(jù)在內(nèi)存中占用較長的時間。

下面就一起來看下怎么用 yield 關(guān)鍵字吧。

一、yield 關(guān)鍵字的使用

1.1 yield return:在迭代中一個一個返回待處理的值

如下示例,循環(huán)輸出小于 9 的偶數(shù),并記錄執(zhí)行任務(wù)的線程 ID:

class Program
{
    static async Task Main(string[] args)
    {
        foreach (int i in ProduceEvenNumbers(9))
        {
            ConsoleExt.Write($"{i}-Main");
        }
        ConsoleExt.Write($"--Main-循環(huán)結(jié)束");
        Console.ReadLine();
    }
    static IEnumerable<int> ProduceEvenNumbers(int upto)
    {
        for (int i = 0; i <= upto; i += 2)
        {
            ConsoleExt.Write($"{i}-ProduceEvenNumbers");
            yield return i;
            ConsoleExt.Write($"{i}-ProduceEvenNumbers-yielded");
        }
        ConsoleExt.Write($"--ProduceEvenNumbers-循環(huán)結(jié)束");
    }
}
public static class ConsoleExt
{
    public static void Write(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static void WriteLine(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static async void WriteLineAsync(object message)
    {
        await Task.Run(() => Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} "));
    }
}

輸出結(jié)果如下,可見整個循環(huán)是單線程運(yùn)行,ProduceEvenNumbers()生產(chǎn)一個,然后Main()就操作一個,Main() 執(zhí)行一次操作后,線程返回生產(chǎn)線,繼續(xù)沿著 return 往后執(zhí)行;生產(chǎn)線循環(huán)結(jié)束后,Main() 也接著結(jié)束:

1.2 yield break:標(biāo)識迭代中斷

如下示例代碼,通過條件中斷循環(huán):

class Program
{
    static void Main()
    {
        ConsoleExt.Write(string.Join(" ", TakeWhilePositive(new[] { 2, 3, 4, 5, -1, 3, 4 })));
        ConsoleExt.Write(string.Join(" ", TakeWhilePositive(new[] { 9, 8, 7 })));
        Console.ReadLine();
    }
    static IEnumerable<int> TakeWhilePositive(IEnumerable<int> numbers)
    {
        foreach (int n in numbers)
        {
            if (n > 0) // 遇到負(fù)數(shù)就中斷循環(huán)
            {
                yield return n;
            }
            else
            {
                yield break;
            }
        }
    }
}
public static class ConsoleExt
{
    public static void Write(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static void WriteLine(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static async void WriteLineAsync(object message)
    {
        await Task.Run(() => Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} "));
    }
}

輸出結(jié)果,第一個數(shù)組中第五個數(shù)為負(fù)數(shù),因此至此就中斷循環(huán),包括它自己之后的數(shù)字不再返回:

1.3 返回類型為 IAsyncEnumerable<T> 的異步迭代器

 實(shí)際上,不僅可以像前邊示例中那樣返回類型為 IEnumerable<T>,還可以使用 IAsyncEnumerable<T> 作為迭代器的返回類型,使得迭代器支持異步。

 如下示例代碼,使用 await foreach 語句對迭代器的結(jié)果進(jìn)行異步迭代:(關(guān)于 await foreach 還有另外一個示例可參考 3.2 await foreach() 示例

class Program
{
    public static async Task Main()
    {
        await foreach (int n in GenerateNumbersAsync(5))
        {
            ConsoleExt.Write(n);
        }
        Console.ReadLine();
    }
    static async IAsyncEnumerable<int> GenerateNumbersAsync(int count)
    {
        for (int i = 0; i < count; i++)
        {
            yield return await ProduceNumberAsync(i);
        }
    }
    static async Task<int> ProduceNumberAsync(int seed)
    {
        await Task.Delay(1000);
        return 2 * seed;
    }
}
public static class ConsoleExt
{
    public static void Write(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static void WriteLine(object message)
    {
        Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} ");
    }
    public static async void WriteLineAsync(object message)
    {
        await Task.Run(() => Console.WriteLine($"(Time: {DateTime.Now.ToString("HH:mm:ss.ffffff")},  Thread {Thread.CurrentThread.ManagedThreadId}): {message} "));
    }
}

輸出結(jié)果如下,可見輸出的結(jié)果有不同線程執(zhí)行:

1.4 迭代器的返回類型可以是 IEnumerator<T> 或 IEnumerator

以下示例代碼,通過實(shí)現(xiàn) IEnumerable<T> 接口、GetEnumerator 方法,返回類型為 IEnumerator<T>,來展現(xiàn) yield 關(guān)鍵字的一個用法:

class Program
{
    public static void Main()
    {
        var ints = new int[] { 1, 2, 3 };
        var enumerable = new MyEnumerable<int>(ints);
        foreach (var item in enumerable)
        {
            Console.WriteLine(item);
        }
        Console.ReadLine();
    }
}
public class MyEnumerable<T> : IEnumerable<T>
{
    private T[] items;
    public MyEnumerable(T[] ts)
    {
        this.items = ts;
    }
    public void Add(T item)
    {
        int num = this.items.Length;
        this.items[num + 1] = item;
    }
    public IEnumerator<T> GetEnumerator()
    {
        foreach (var item in this.items)
        {
            yield return item;
        }
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

1.5 不能使用 yield 的情況

1.yield return 不能套在 try-catch 中;

2.yield break 不能放在 finally 中;

3.yield 不能用在帶有 in、ref 或 out 參數(shù)的方法;

4.yield 不能用在 Lambda 表達(dá)式和匿名方法;

5.yield 不能用在包含不安全的塊(unsafe)的方法。

https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/statements/yield 

二、使用 yield 關(guān)鍵字實(shí)現(xiàn)惰性枚舉

在 C# 中,可以使用 yield 關(guān)鍵字來實(shí)現(xiàn)惰性枚舉。惰性枚舉是指在使用枚舉值時,只有在真正需要時才會生成它們,這可以提高程序的性能,因?yàn)樵诓恍枰褂妹杜e值時,它們不會被生成或存儲在內(nèi)存中。

當(dāng)然對于簡單的枚舉,實(shí)際上還沒普通的 List<T> 有優(yōu)勢,因?yàn)槿∶杜e值也會對性能有損耗,所以只針對處理大型集合或延遲加載數(shù)據(jù)才能看到效果。

下面是一個簡單示例,展示了如何使用 yield 關(guān)鍵字來實(shí)現(xiàn)惰性枚舉:

public static IEnumerable<int> enumerableFuc()
{
    yield return 1;
    yield return 2;
    yield return 3;
}
// 使用惰性枚舉
foreach (var number in enumerableFuc())
{
    Console.WriteLine(number);
}

在上面的示例中,GetNumbers() 方法通過yield關(guān)鍵字返回一個 IEnumerable 對象。當(dāng)我們使用 foreach 循環(huán)迭代這個對象時,每次循環(huán)都會調(diào)用 MoveNext() 方法,并執(zhí)行到下一個 yield 語句處,返回一個元素。這樣就實(shí)現(xiàn)了按需生成枚舉的元素,而不需要一次性生成所有元素。

三、通過 IL 代碼看 yield 的原理

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

static async Task Main(string[] args)
{
    // 用 while (enumerator.MoveNext()) 
    // 代替 foreach(int item in enumerableFuc())
    IEnumerator<int> enumerator = enumerableFuc().GetEnumerator();
    while (enumerator.MoveNext())
    {
        int current = enumerator.Current;
        Console.WriteLine(current);
    }
    Console.ReadLine();
}
// 一個返回類型為 IEnumerable<int>,其中包含三個 yield return
public static IEnumerable<int> enumerableFuc()
{
    Console.WriteLine("enumerableFuc-yield 1");
    yield return 1;
    Console.WriteLine("enumerableFuc-yield 2");
    yield return 2;
    Console.WriteLine("enumerableFuc-yield 3");
    yield return 3;
}

輸出的結(jié)果:

下面試著簡單看一下 Program 類的源碼

源碼如下,除了明顯的 Main() 和 enumerableFuc() 兩個函數(shù)外,反編譯的時候自動生成了一個新的類 '<enumerableFuc>d__1'。

注:反編譯時,語言選擇:“IL with C#”,有助于理解。

然后看自動生成的類的實(shí)現(xiàn),發(fā)現(xiàn)它繼承了 IEnumerable、IEnumerable<T>、IEnumerator、IEnumerator<T>,也實(shí)現(xiàn)了MoveNext()、Reset()、GetEnumerator()、Current 屬性,這時我們應(yīng)該可以確認(rèn),這個新的類,就是我們雖然沒有實(shí)現(xiàn)對應(yīng)的 IEnumerator 的 MoveNext() 和 Current 屬性,但是我們?nèi)匀荒苷J褂眠@些函數(shù)的原因了。

然后再具體看下 MoveNext() 函數(shù),根據(jù)輸出的備注字段,也能清晰的看到迭代過程,下圖中紫色部分:

下邊是是第三、四次迭代,可以看到行標(biāo)識可以對得上:

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

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

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

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

到此這篇關(guān)于詳解C#中yield關(guān)鍵字的用法的文章就介紹到這了,更多相關(guān)C# yield內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論