IEnumerable、GetEnumerator、IEnumerator的理解

概念文字性的东西,我们就不说了,这里我们来点具体的实例第呀;

实例一:

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

namespace Pratice001
{
   public  class Test:IEnumerable
    {
        private static int count = 12;
        private string[] elements;
        private int ctr = 0; //数组下标的计数器;

        public Test(params string[] initialString)
        {
            elements = new String[8];
            foreach (string s in initialString)
            {
                elements[ctr++] = s; //数据初始化
            }
        }
       public  Test(string initialString, char[] delimiters)
        {
            elements = initialString.Split(delimiters); //不同的方式 进行初始化;
        }

        //实现接口中的方法;
        public IEnumerator GetEnumerator()
        {
            return new ForeachTestEnumerator(this);
        }

        public class ForeachTestEnumerator : IEnumerator
        {
            private int position = -1;
            private Test t;
            public ForeachTestEnumerator(Test t)
            {
                this.t = t;
            }

            //实现接口;
            public object Current
            {
                get
                {
                    return t.elements[position];
                }
            }

            public bool MoveNext()
            {
                if (position < t.elements.Length - 1)
                {
                    position++;
                    return true;
                }
                else
                {
                   return  false;
                }
            }

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

        }

    }

}

实例二:

然后我们再来一个实例!

using System;
using System.Collections;

public class Person
{
    public Person(string fName, string lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public string firstName;
    public string lastName;
}

public class People : IEnumerable
{
    private Person[] _people;
    public People(Person[] pArray)
    {
        _people = new Person[pArray.Length];

        for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
       return (IEnumerator) GetEnumerator();
    }

    public PeopleEnum GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
}

public class PeopleEnum : IEnumerator
{
    public Person[] _people;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public PeopleEnum(Person[] list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Length);
    }

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

    object IEnumerator.Current
    {
        get
        {
            return Current;
        }
    }

    public Person Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

class App
{
    static void Main()
    {
        Person[] peopleArray = new Person[3]
        {
            new Person("John", "Smith"),
            new Person("Jim", "Johnson"),
            new Person("Sue", "Rabon"),
        };

        People peopleList = new People(peopleArray);
        foreach (Person p in peopleList)
            Console.WriteLine(p.firstName + " " + p.lastName);

    }
}

/* This code produces output similar to the following:
 *
 * John Smith
 * Jim Johnson
 * Sue Rabon
 *
 */

最好,跟代码,一步步的调试出来看看的呀;

时间: 2024-08-04 00:57:04

IEnumerable、GetEnumerator、IEnumerator的理解的相关文章

深入理解 IEnumerable和IEnumerator

刚开始接触的C#的时候,就一直被IEnumerable.IEnumerator.ICollection搞混,所以我就彻底的弄清楚他们的本质关系. 一讲到IEnumerable很自然的想到IEnumerator.foreach等.他们确实有很多密切的关系,今天我就详细的讲解一下这几方面的关系,IEnumerable接口中只有一个抽象方法 GetEnumerator(),他返回一个可访问循环集合的IEnumerator对象.该对象才是真正的集合访问器.只要有了它,foreach才能进行循环遍历数组或

《转》IEnumerable、IEnumerator两个接口的认识

前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下. 查看并使用两个接口 接下来我们先来看看两个接口的定义. 先来看一下IEnumerable接口,其实看过这个接口之后,发现它其实是非常的简单,只包含一个方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象,如下面截图所示: 这里的IEnumerator对象,其实就是另外一个接口,这个接口对象有什么呢

IList,ICollection,IEnumerable,IEnumerator,IQueryable

1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Reset(); while (myie.MoveNext()) { int i = (int)myie.Current; Console.WriteLine("Value: {0}", i); } 相信很多人都不会像上面这样去遍历myArray这个数组,通常我们这样会这样做 foreach

[转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Reset(); while (myie.MoveNext()) { int i = (int)myie.Current; Console.WriteLine("Value: {0}", i); } 相信很多人都不会像上面这样去遍历myArray这个数组,通常我们这样会这样做 foreach

IList, ICollection ,IEnumerable AND IEnumerator in C#

IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: 1 // 摘要: 2 // 表示可按照索引单独访问的对象的非泛型集合. 3 [ComVisible(true)] 4 public interface IList : ICollection, IEnumerable 5 { 6 7 bool IsFixedSize { get; } 8 9 bool IsReadOnly { get; } 10 11 object this[int i

【5min+】你怎么穿着品如的衣服?IEnumerable AND IEnumerator

系列介绍 简介 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net知识等等. 场景 您可以在下班坐地铁的时候,拿出手机逛一逛博客园,利用短短的五分钟完成阅读. 诞生缘由 曾经学过的内容可能过不了多久就忘了,我们需要一些文章来帮我们查漏补缺. 太长篇幅的文章看着滚动条就害怕了,我们可能更期望文字少的文章. .net体系的内容太多了,平时也不知道该学哪些,我们可

IEnumerable和IEnumerator 详解

http://blog.csdn.net/byondocean/article/details/6871881 初学C#的时候,老是被IEnumerable.IEnumerator.ICollection等这样的接口弄的糊里糊涂,我觉得有必要切底的弄清楚IEnumerable和IEnumerator的本质. 下面我们先看IEnumerable和IEnumerator两个接口的语法定义.其实IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于

C# IEnumerable和IEnumerator的区别,如何实现

IEnumerable接口和IEnumerator接口是.NET中非常重要的接口,二者有何区别? 1. 简单来说IEnumerable是一个声明式的接口,声明实现该接口的类就是"可迭代的enumerable",但并没用说明如何实现迭代器(iterator).其代码实现为: public interface IEnumerable         {                IEnumerator GetEnumerator();          }    2. 而IEnumer

关于迭代器中IEnumerable与IEnumerator的区别

首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumerator接口是一个真正的集合访问器,它包含MoveNext()方法和Current属性,在foreach循环中,如果MoveNext()返回True,则就是用IEnumerator接口的Current属性来获取对象的一个引用,用于foreach循环. 下面是自定义的一个迭代器的例子: Primer.