C#foreach的原理

foreach是对集合进行遍历,而集合都继承Array类
foreach结构设计用来和可枚举类型一起使用,只要给它的遍历对象是可枚举类型
只有实现了IEnumerable接口的类(也叫做可枚举类型)才能进行foreach的遍历操作,集合和数组已经实现了这个接口,所以能进行foreach的遍历操作
集合继承IEnumerable这个接口,而IEnumerable的 IEnumerator GetEnumerator()方法 可以获得一个可用于循环访问集合的 对象


        List<string> list = new List<string>() { "252", "哈哈哈", "25256", "春天里的花朵" };
        Console.WriteLine(list.GetType().Name);

        IEnumerator listEnumerator = list.GetEnumerator();
        while (listEnumerator.MoveNext())
        {
            Console.WriteLine(listEnumerator.Current);
        }
        listEnumerator.Reset();

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 枚举器
{
///
/// 枚举器泛型类
///
class Enumerator : IEnumerator
{
private T[] array;
private int _postion = -1;

    public Enumerator(T[] sum)
    {
        array = new T[sum.Length];
        for (int i = 0; i < sum.Length; i++)
        {
            array[i] = sum[i];
        }
    }

    public void Dispose()
    {

    }

    /// <summary>
    /// 把枚举器的位置前进到下一项,返回布尔值,
    /// 新的位置若是有效的,返回true,否则返回false
    /// </summary>
    /// <returns></returns>
    public bool MoveNext()
    {
        if (this._postion>=this.array.Length)
        {
            return false;
        }
        else
        {
            this._postion++;
            return _postion < this.array.Length;
        }
    }

    /// <summary>
    /// 将位置重置为原始状态
    /// </summary>
    public void Reset()
    {
        this._postion = -1;
    }

    public T Current
    {
        get
        {
            if (this._postion == -1)
            {
                throw new InvalidOperationException();
            }


            return array[this._postion];
        }

    }

    object IEnumerator.Current
    {
        get
        {
            if (this._postion == -1)
            {
                throw new InvalidOperationException();
            }

            return array[_postion];
        }
    }
}

}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 枚举器
{
///
///可枚举类是指实现了IEnumerble接口的类 它里面只有一个方法,Getenumerator()
///它可以获得对象的枚举器
///
///
class enumable:IEnumerable
{
private T[] sum ;

    public enumable(T[] array)
    {
        sum =new T[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            sum[i] = array[i];
        }
    }
    public IEnumerator<T> GetEnumerator()
    {
       return new Enumerator<T>(sum);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new Enumerator<T>(sum);
    }
}

}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 枚举器
{
class Program
{
static void Main(string[] args)
{

        //可枚举类是指实现了IEnumerble接口的类 它里面只有一个方法,Getenumerator()
       //它可以获得对象的枚举器

        int[] sum = {5, 8, 5, 8, 11};
        string[] array = {"哈哈", "ii", "5222"};
        enumable<int> intenumable=new enumable<int>(sum);
        foreach (int temp in intenumable)
        {
            Console.Write(temp + "\t");
        }

        Console.WriteLine();
        enumable<string> stringEnumable=new enumable<string>(array);
        foreach (string temp in stringEnumable)
        {
            Console.Write(temp+"\t");
        }
        Console.ReadKey();
    }
}

}

猜你喜欢

转载自blog.csdn.net/weixin_33950757/article/details/88530467
今日推荐