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

C# IEnumerator枚舉器的具體使用

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

1、對象只要一個類型實現(xiàn)了IEnumerable接口就能遍歷
2、IEnumerator是枚舉器,一個接口類,實現(xiàn)MoveNext->Current->Reset
3、yield關(guān)鍵字是一個迭代器,相當于實現(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() { //返回一個枚舉器
? ? ? ? ? ? //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是可枚舉的意思。這兩個都是個接口

foreach是一種語法糖,用來簡化對可枚舉元素的遍歷代碼。而被遍歷的類通過實現(xiàn)IEnumerable接口和一個相關(guān)的IEnumerator枚舉器來實現(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()負責獲取枚舉器。
MoveNext()負責讓Current獲取下一個值,并判斷遍歷是否結(jié)束。
Current負責返回當前指向的值。
Rest()負責重置枚舉器的狀態(tài)(在foreach中沒有用到)
這些就是IEnumerable,IEnumerator的基本工作原理了。

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

等價于

        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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論