能使用foreach语句进行遍历的必须是实现了IEnuerable接口的类型,IEnumerable只有一个方法:IEnumerator GetEnumerator();IEnumerator有三个方法 object Current()//获取当前对象,bool MoveNext()//将游标移到下一个位置,void Reset()//将光标重置的第一个成员前面。以下是我写的一个例子。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace ForeachAble
{
    class Program
    {
        static void Main(string[] args)
        {
            CarStore _4s= new CarStore();
            foreach (Car item in _4s)
            {
                Console.WriteLine(item.CarName); 
            }
            Console.ReadKey();
        }
    }    public class Car
    {
        private string _name;
        public string CarName
        {
            get
            {
                return this._name;
            }
            set
            {
                _name = value;
            }
        }        public Car(string name)
        {
            this._name = name;
        }
        
    }
    public class CarStore:IEnumerable
    {
        private Car[] _cars;
        public Car[] Cars
        {
            get
            {
                return _cars;
            }
            set
            {
                _cars = value;
            }
        }        public CarStore()
        {
            _cars = new Car[] { new Car("吉利"),new Car("比亚迪"),new Car("力帆"),new Car("奔腾B70"),new Car("红旗")};
        }        public IEnumerator GetEnumerator()
        {
            return new CarEnumertor(this);
        }
        
        public class CarEnumertor : IEnumerator
        {
            int _index;
            CarStore _newCarStore;
            public CarEnumertor(CarStore newCarStore)
            {
                _index = -1;
                _newCarStore = newCarStore;
            }
            public object Current
            {
                get { return _newCarStore._cars[_index]; }
            }            public bool MoveNext()
            {
                if (_index < _newCarStore._cars.Length-1)
                {
                    _index++;
                    return true;
                }
                else
                {
                    return false;
                }
            }            public void Reset()
            {
                _index=-1;
            }
        }
}
    
}
运行通过,显示出了5个car对象,但有一个疑惑的是关于 Reset方法,无论我将_index的值改为-1,0,1,甚至什么都不做,都没有影响最后结果,好像Reset方法没有执行一样。反而是构造函数的_index赋值结果会影响执行结果,将_index=0,第一个car对象就不会显示。不知为什么?

解决方案 »

  1.   

    补充一下,我想问的是为什么Reset方法没有执行。
      

  2.   

    你的例子没有调用Reset,当然Reset没有起任何作用了。
    CarStore cars = new CarStore();
    IEnumerator ienum = cars.GetEnumerator();Car car;
    if (ienum.MoveNext()) car = ienum.Current as Car; // car = 吉利
    if (ienum.MoveNext()) car = ienum.Current as Car; // car = 比亚迪ienum.Reset();                                    // <--------
    if (ienum.MoveNext()) car = ienum.Current as Car; // car = 吉利
      

  3.   

    你没有地方调用Reset(),当然不执行。
      

  4.   

    你的Main修改为这样试试。static void Main(string[] args)
    {
        CarStore _4s = new CarStore();
        IEnumerator e = _4s.GetEnumerator();
        while (e.MoveNext())
        {
            Console.WriteLine((e.Current as Car).CarName);
        }
        Console.WriteLine(e.MoveNext());
        e.Reset();
        Console.WriteLine(e.MoveNext());
        Console.ReadKey();
    }
      

  5.   

    你没有地方调用Reset(),当然不执行啦,,,,
      

  6.   

    首先IEnumerator接口实现枚举器的工作,默认情况你使用foreach仅仅遍历了一次就丢弃了;但是如果你想在一段代码中多次遍历一串对像时,就必须显式获取IEnumerator的对象实例,在每次遍历前显式调用Reset方法。foreach就是通过IEnuerable获取了一次性的IEnumerator对象实例,用完就丢弃了。因此不存在Reset之说。
      

  7.   

    谢谢各位了,我原以为,Reset()方法是会被foreach语句自动调用的。看来这里还是有些热心的高手的,拜谢各位了!几位说的都不错只能平均点给分了。
      

  8.   

    Reset()方法是会被foreach语句自动调用的