日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 中的yield使用

發(fā)布時(shí)間:2024/9/20 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 中的yield使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

yield是C#為了簡化遍歷操作實(shí)現(xiàn)的語法糖,我們知道如果要要某個(gè)類型支持遍歷就必須要實(shí)現(xiàn)系統(tǒng)接口IEnumerable,這個(gè)接口后續(xù)實(shí)現(xiàn)比較繁瑣要寫一大堆代碼才能支持真正的遍歷功能。舉例說明

using?System;
using?System.Collections.Generic;
using?System.Collections;
using?System.Linq;
using?System.Text;
namespace
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
????????????HelloCollection?helloCollection?=?new?HelloCollection();
????????????foreach?(string?s?in?helloCollection)
????????????{
????????????????Console.WriteLine(s);
????????????}
????????????Console.ReadKey();
????????}
????}
????//
public?class?HelloCollection?:?IEnumerable
????//
{
????//
????public?IEnumerator?GetEnumerator()
????//
????{
????//
????????yield?return?"Hello";
????//
????????yield?return?"World";
????//
????}
????//}
????public?class?HelloCollection?:?IEnumerable
????{
????????public?IEnumerator?GetEnumerator()
????????{
????????????Enumerator?enumerator?=?new?Enumerator(0);
????????????return?enumerator;
????????}
????????public?class?Enumerator?:?IEnumerator,?IDisposable
????????{
????????????private?int?state;
????????????private?object?current;
????????????public?Enumerator(int?state)
????????????{
????????????????this.state?=?state;
????????????}
????????????public?bool?MoveNext()
????????????{
????????????????switch?(state)
????????????????{
????????????????????case?0:
????????????????????????current?=?"Hello";
????????????????????????state?=?1;
????????????????????????return?true;
????????????????????case?1:
????????????????????????current?=?"World";
????????????????????????state?=?2;
????????????????????????return?true;
????????????????????case?2:
????????????????????????break;
????????????????}
????????????????return?false;
????????????}
????????????public?void?Reset()
????????????{
????????????????throw?new?NotSupportedException();
????????????}
????????????public?object?Current
????????????{
????????????????get?{?return?current;?}
????????????}
????????????public?void?Dispose()
????????????{
????????????}
????????}
????}
}

??? 上面注釋的部分引用了"yield return”,其功能相當(dāng)于下面所有代碼!可以看到如果不適用yield需要些很多代碼來支持遍歷操作。

??? yield return 表示在迭代中下一個(gè)迭代時(shí)返回的數(shù)據(jù),除此之外還有yield break, 其表示跳出迭代,為了理解二者的區(qū)別我們看下面的例子

class?A?:?IEnumerable
{
? ??private int[] array = new int[10] {1, 1, 2, 3, 4, 5, 6, 7, 87, 10};
????public?IEnumerator?GetEnumerator()
????{
????????for?(int?i?=?0;?i?<?10;?i++)
????????{
????????????yield?return?array[i];
????????}
????}
}

??? 如果你只想讓用戶訪問ARRAY的前8個(gè)數(shù)據(jù),則可做如下修改.這時(shí)將會用到y(tǒng)ield break,修改函數(shù)如下

public?IEnumerator?GetEnumerator()
{
????for?(int?i?=?0;?i?<?10;?i++)
????{
????????if?(i?<?8)
????????{
????????????yield?return?array[i];
????????}
????????else
????????{
????????????yield?break;
????????}
????}
}

??? 這樣,則只會返回前8個(gè)數(shù)據(jù).

總結(jié)

以上是生活随笔為你收集整理的C# 中的yield使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。