一、关于枚举的含义

.Net提供了可枚举类型的接口IEnumerable和枚举器接口IEnumerator,程序集System.Collections

另:

IQueryable 继承自IEnumerable(System.Core)

枚举,又叫列举,顾名思义,(程序)一个一个列举出来。列举出来以方便查询应用(linq)。常见的,我们遍历一个集合(Collection)的时候,foreach不可少,在C#中有些是可以被foreach(linq查询,foreach本质是linq查询执行方法),这些数据结构

有一个特征:可枚举,像数组,List, ArryList,等,已经隐式实现了接口IEnumerable,可以返回一个IEnumerator(iteror枚举器)。

二、两个接口的定义

public interface IEnumerable
{
        //IEnumerable只有一个方法,返回可循环访问集合的枚举数。
        IEnumerator GetEnumerator() ;
}

public interface IEnumerator
{
        // 方法
        //移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false。
        bool MoveNext();
       // 将集合设置为初始位置,该位置位于集合中第一个元素之前
       void Reset();
      // 属性:获取集合中的当前元素
      object Current { get; }
}

二、二者的区别和联系

1、一个集合可查询(可用foreach,where,any等),必须以某种方式返回IEnumerator object)也就是必须实现IEnumerable接口

2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。

3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的

4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接。

三、具体实例

namespace ConsoleApp8
{
    public class Person
    {
        public string Name
        {
            set;
            get;
        }
        public Person(string name)
        {
            Name = name;
        }
    }

    public class PeopleEnum : IEnumerator
    {
        public Person[] _people;
        int position = -1;
        public PeopleEnum(Person[] list)
        {
            _people = list;
        }
        public bool MoveNext()
        {
            position++;
            return (position < _people.Length);
        }
        public void Reset()
        {
            position = -1;
        }
        public object Current
        {
            get
            {
                try
                {
                    return _people[position];
                }
                catch (IndexOutOfRangeException)
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
    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];
            }
        }
        public IEnumerator GetEnumerator()
        {
            return new PeopleEnum(people);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person[] people = new Person[4]
            {
                new Person("李磊"),
                new Person("王刚"),
                new Person("彤彤"),
                new Person("丹丹"),
            };

            People listPeople = new People(people);

            foreach (var p in listPeople)
            {
                Console.WriteLine(((Person)p).Name);
            }

            Console.ReadLine();

        }
    }
}

  欢迎qq加群学习:568055323

相关文章:

  • 2021-10-12
  • 2021-08-25
  • 2021-07-23
  • 2021-11-19
猜你喜欢
  • 2021-09-09
  • 2021-07-05
  • 2021-07-25
  • 2022-12-23
  • 2021-10-23
  • 2021-11-17
  • 2022-12-23
相关资源
相似解决方案