IEnumerable 接口

公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。

[GuidAttribute("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
[ComVisibleAttribute(true)]
public interface IEnumerable


  名称 说明
System_CAPS_pubmethod GetEnumerator()

返回循环访问集合的枚举数。

  名称 说明
System_CAPS_pubmethod AsParallel()

已重载。启用查询的并行化。(由 ParallelEnumerable 定义。)

System_CAPS_pubmethod AsQueryable()

已重载。将转换 IEnumerable 到 IQueryable(由 Queryable 定义。)

System_CAPS_pubmethod Cast<TResult>()

将强制转换的元素 IEnumerable 为指定的类型。(由 Enumerable 定义。)

System_CAPS_pubmethod OfType<TResult>()

筛选的元素 IEnumerable 根据指定的类型。(由 Enumerable 定义。)

IEnumerable 是可以枚举的所有非泛型集合的基接口。IEnumerable包含单个方法GetEnumerator,它将返回IEnumerator IEnumerator提供了通过公开循环访问集合的能力Current属性和MoveNextReset方法。

若要实现最佳做法是IEnumerableIEnumerator上您的集合类,以启用foreach(For Each在 Visual Basic 中) 语法,但是实现IEnumerable不是必需的。 如果你的集合不实现IEnumerable,您仍必须按照要通过提供支持此语法的迭代器模式GetEnumerator返回接口、 类或结构的方法。 如果使用 Visual Basic,则必须提供IEnumerator实现,它可以返回GetEnumerator 使用 C# 开发时必须提供一个包含类Current属性,和MoveNextReset方法如下所述IEnumerator,但类不必实现IEnumerator


下面的代码示例演示如何循环通过实现自定义集合的最佳做法IEnumerableIEnumerator接口。 在此示例中,不显式调用这些接口的成员,但它们并实现以支持使用foreach(For Each在 Visual Basic 中) 以循环访问集合。 此示例是一个完整的控制台应用。

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


namespace IEnumerableTest
{


    // 简单的业务对象。
    public class Person
    {
        public Person(string fName, string lName)
        {
            this.firstName = fName;
            this.lastName = lName;
        }


        public string firstName;
        public string lastName;
    }


    //收集Person对象。这个类
    //实现可使用的可被使用的
    //用ForEach语法。
    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];
            }
        }


        // GetEnumerator方法的实现。
        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)GetEnumerator();
        }


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




    // 当你实现 IEnumerable, 你必须也实现 implement IEnumerator.
    public class PeopleEnum : IEnumerator
    {
        public Person[] _people;


        //枚举器在第一个元素之前被定位
        //直到第一个MoveNext()调用。
        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 Program
    {
        static void Main22(string[] args)
        {
            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);
            }
            Console.ReadKey();
        }
    }
}







猜你喜欢

转载自blog.csdn.net/luochenlong/article/details/79310783