最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# IEnumerator枚舉器的具體使用

 更新時(shí)間:2022年06月23日 10:54:46   作者:霍比特X  
本文主要介紹了C# IEnumerator枚舉器的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、對(duì)象只要一個(gè)類型實(shí)現(xiàn)了IEnumerable接口就能遍歷
2、IEnumerator是枚舉器,一個(gè)接口類,實(shí)現(xiàn)MoveNext->Current->Reset
3、yield關(guān)鍵字是一個(gè)迭代器,相當(dāng)于實(shí)現(xiàn)了IEnumerator枚舉器
4、IEnumerable是可枚舉類型,IEnumerator是枚舉器

public class IEnumerableShow {
? ? ? ? public void Show() {
? ? ? ? ? ? int[] array = { 1, 2, 3, 4, 5 };
? ? ? ? ? ? Student student = new Student { Id = 1 };
? ? ? ? ? ? foreach (var item in array) {
? ? ? ? ? ? ? ? Console.WriteLine();
? ? ? ? ? ? }
? ? ? ? }
? ? }

? ? class Student:IEnumerable {?
? ? ? ? public int Id { get; set; }
? ? ? ? public IEnumerator GetEnumerator() { //返回一個(gè)枚舉器
? ? ? ? ? ? //yield return "Ant編程1";
? ? ? ? ? ? //yield return "Ant編程2";
? ? ? ? ? ? //yield return "Ant編程3";
? ? ? ? ? ? string[] student = { "Ant編程1", "Ant編程2", "Ant編程3" };
? ? ? ? ? ? return new StudentEnumerator(student);
? ? ? ? }
? ? }

? ? internal class StudentEnumerator : IEnumerator
? ? {
? ? ? ? string[] _student;
? ? ? ? int _position = -1;
? ? ? ? public StudentEnumerator(string[] student) {
? ? ? ? ? ? this._student = student;
? ? ? ? }

? ? ? ? public object Current {
? ? ? ? ? ? get {
? ? ? ? ? ? ? ? if (_position == -1) {
? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (_position>=_student.Length) {
? ? ? ? ? ? ? ? ? ? throw new InvalidOperationException();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return _student[_position];
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? public bool MoveNext()
? ? ? ? {
? ? ? ? ? ? if (_position<_student.Length-1) {
? ? ? ? ? ? ? ? _position++;
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? public void Reset()
? ? ? ? {
? ? ? ? ? ? _position = -1;
? ? ? ? }
? ? }

IEnumerator , IEnumerable 枚舉器接口

IEnumerator是枚舉器的意思,IEnumerable是可枚舉的意思。這兩個(gè)都是個(gè)接口

foreach是一種語法糖,用來簡(jiǎn)化對(duì)可枚舉元素的遍歷代碼。而被遍歷的類通過實(shí)現(xiàn)IEnumerable接口和一個(gè)相關(guān)的IEnumerator枚舉器來實(shí)現(xiàn)遍歷功能。

public class MyList : IEnumerable
{
    public int[] _data = new int[10] { 1, 5, 7, 9, 7, 8, 7, 8, 7, 4 };
 
    public int this[int index]
    {
        get
        {
            return _data[index];
        }
    }
 
    IEnumerator IEnumerable.GetEnumerator()
    {
        Debug.Log("foreach調(diào)用  GetEnumerator");
        return new MIEnumtor(this);
    }
}
public class MIEnumtor : IEnumerator
{
    private MyList myList;
 
    private int index;
 
   public  MIEnumtor(MyList my)
    {
        index = -1;
        myList = my;
    }
 
    public object Current
    {
        get
        {
            Debug.Log("foreach調(diào)用  Current");
            return myList[index];
        }
    }
 
    public bool MoveNext()
    {
        Debug.Log("foreach調(diào)用  MoveNext");
        if (index < myList._data.Length - 1)
        {
            index++;
            return true;
        }
        index = -1;
        return false;
    }
 
    public void Reset()
    {
 
    }
}

GetIEnumerator()負(fù)責(zé)獲取枚舉器。
MoveNext()負(fù)責(zé)讓Current獲取下一個(gè)值,并判斷遍歷是否結(jié)束。
Current負(fù)責(zé)返回當(dāng)前指向的值。
Rest()負(fù)責(zé)重置枚舉器的狀態(tài)(在foreach中沒有用到)
這些就是IEnumerable,IEnumerator的基本工作原理了。

        MyList my = new MyList();
 
        foreach (var item in my)
        {
            Debug.Log(item);
        }

等價(jià)于

        MyList my = new MyList();
 
        MIEnumtor mIEnumtor = my.GetEnumerator();
 
        while (mIEnumtor.MoveNext())
        {
            Debug.Log(mIEnumtor.Current);
        }

到此這篇關(guān)于C# IEnumerator枚舉器的具體使用的文章就介紹到這了,更多相關(guān)C# IEnumerator枚舉器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié)

    C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié)

    這篇文章主要介紹了C#中后臺(tái)post請(qǐng)求常用的兩種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 使用C#在Excel工作表中查找與替換數(shù)據(jù)的操作指南

    使用C#在Excel工作表中查找與替換數(shù)據(jù)的操作指南

    在日常業(yè)務(wù)中,Excel 常用于保存產(chǎn)品清單、客戶信息、銷售記錄等結(jié)構(gòu)化數(shù)據(jù),本文將介紹如何使用 C# 在 Excel 中高效完成查找與替換操作,并結(jié)合一個(gè)實(shí)際業(yè)務(wù)場(chǎng)景表格數(shù)據(jù)進(jìn)行演示,示例涵蓋從加載文件、查找目標(biāo)數(shù)據(jù)到替換與保存完整流程,需要的朋友可以參考下
    2025-11-11
  • C#利用棧實(shí)現(xiàn)加減乘除運(yùn)算

    C#利用棧實(shí)現(xiàn)加減乘除運(yùn)算

    這篇文章主要介紹了C#利用棧實(shí)現(xiàn)加減乘除運(yùn)算的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-11-11
  • C#實(shí)現(xiàn)Quartz任務(wù)調(diào)度的示例代碼

    C#實(shí)現(xiàn)Quartz任務(wù)調(diào)度的示例代碼

    使用 Quartz.NET,你可以很容易地安排任務(wù)在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行,或者每天、每周、每月的特定時(shí)間運(yùn)行,甚至可以基于更復(fù)雜的調(diào)度規(guī)則,本文給大家介紹了C#實(shí)現(xiàn)Quartz任務(wù)調(diào)度,需要的朋友可以參考下
    2024-04-04
  • WPF實(shí)現(xiàn)基礎(chǔ)控件之托盤的示例代碼

    WPF實(shí)現(xiàn)基礎(chǔ)控件之托盤的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用WPF實(shí)現(xiàn)托盤這一基礎(chǔ)控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-10-10
  • c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享

    c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享

    這篇文章主要介紹了使用c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例,需要的朋友可以參考下
    2014-02-02
  • c# 動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式

    c# 動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式

    這篇文章主要介紹了c# 如何動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-11-11
  • C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析

    C#日期格式字符串的相互轉(zhuǎn)換操作實(shí)例分析

    這篇文章主要介紹了C#日期格式字符串的相互轉(zhuǎn)換操作,結(jié)合實(shí)例形式分析了C#日期格式字符串的相互轉(zhuǎn)換操作函數(shù)與相關(guān)使用技巧,需要的朋友可以參考下
    2019-08-08
  • Unity Blend Tree動(dòng)畫混合樹使用入門教程

    Unity Blend Tree動(dòng)畫混合樹使用入門教程

    這篇文章主要為大家詳細(xì)介紹了Unity Blend Tree動(dòng)畫混合樹使用入門教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C#中三種Timer計(jì)時(shí)器的詳細(xì)用法

    C#中三種Timer計(jì)時(shí)器的詳細(xì)用法

    這篇文章介紹了C#中三種Timer計(jì)時(shí)器的詳細(xì)用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

德州市| 龙南县| 平和县| 比如县| 闵行区| 淅川县| 崇阳县| 桂阳县| 宾阳县| 济源市| 信宜市| 通榆县| 和顺县| 东源县| 洛宁县| 广灵县| 云和县| 阿巴嘎旗| 襄垣县| 武汉市| 兴化市| 西平县| 东城区| 民权县| 界首市| 佛教| 永修县| 玉山县| 长乐市| 大同县| 松桃| 临沭县| 新闻| 紫金县| 河东区| 榆林市| 从江县| 彰化市| 上林县| 玛纳斯县| 方正县|