您的位置:首页 > 编程语言 > C#

C#IEnumerable和IEnumerator的学习心得

2011-02-24 23:35 246 查看
下面是一个例子:
    /// <summary>
    /// 定义人的类型
    /// </summary>
    public class Person
    {
        public string firstName;
        public string lastName;
        public Person(string first, string last)
        {
            this.firstName = first;
            this.lastName = last;
        }
    }

    /// <summary>
    /// 实现枚举器
    /// </summary>
    public class MyEnum<T> : IEnumerator
    {
        public T[] _array;
        int _position = -1;

        public MyEnum(T[] _array)
        {
            this._array = _array;
        }

        public object Current
        {
            get
            {
                try
                {
                    return _array[_position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new IndexOutOfRangeException();
                }
            }
        }

        public bool MoveNext()
        {
            _position++;
            return (_position < _array.Length);
        }

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

    /// <summary>
    /// 实现一个可枚举的类
    /// </summary>
    public class MyList<T> : IEnumerable
    {
        public T[] _array;
        public MyList(T[] _array)
        {
            this._array = _array;
        }

        //取得枚举器
        public IEnumerator GetEnumerator()
        {
            return new MyEnum<T>(this._array);
        }
    }
    class TestMain
    {
        public static void Main()
        {
            Person[] personArray = {
                                       new Person("John", "Smith"),
                                       new Person("Jim", "Johnson"),
                                       new Person("Sue", "Rabon"),
                                   };
            MyList<Person> list = new MyList<Person>(personArray);
            foreach (Person p in list)
                Console.WriteLine(p.firstName + " " + p.lastName);

            Console.ReadKey();
        }
    }

结论:如果将foreach语句用于集合,就需要IEnumerable接口的GetEnumerator()方法放回一个IEnumerator枚举。

也可以使用yield语句可以快速的实现一个枚举IEnumerator,例子如下:

    /// <summary>
    /// 定义人的类型
    /// </summary>
    public class Person
    {
        public string firstName;
        public string lastName;
        public Person(string first, string last)
        {
            this.firstName = first;
            this.lastName = last;
        }
    }

    /// <summary>
    /// 实现一个可枚举的类
    /// </summary>
    public class MyList<T> : IEnumerable
    {
        public T[] _array;
        public MyList(T[] _array)
        {
            this._array = _array;
        }

        //取得枚举器
        public IEnumerator GetEnumerator()
        {
            foreach (T t in _array)
            {
                yield return t;
            }
        }
    }
    class TestMain
    {
        public static void Main()
        {
            Person[] personArray = {
                                       new Person("John", "Smith"),
                                       new Person("Jim", "Johnson"),
                                       new Person("Sue", "Rabon"),
                                   };
            MyList<Person> list = new MyList<Person>(personArray);
            foreach (Person p in list)
                Console.WriteLine(p.firstName + " " + p.lastName);

            Console.ReadKey();
        }
    }

这个更简单
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string class list object