zoukankan      html  css  js  c++  java
  • IEnumerable 和 IEnumerator

    IEnumerable 接口只包含一个抽象的方法 GetEnumerator(),它返回一个可用于循环访问集合的 IEnumerator 对象,IEnumerator 对象是一个集合访问器。

    需要给自定义的类实现 foreach 功能,就需要实现 IEnumerable 接口,下面给出一个例子。

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    class NewList<T> : IEnumerable          //实现 IEnumerable 接口的 GetEnumerator() 
    {
        private List<T> newList = new List<T>();
    
        public void Add(T item)
        {
            newList.Add(item);
        }
        public IEnumerator GetEnumerator()
        {
            return this.newList.GetEnumerator();
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            NewList<string> newList = new NewList<string>();
            newList.Add("a");
            newList.Add("b");
            foreach(string item in newList)
            {
                Console.WriteLine("item:" + item);
            }
        }
    }

    手工实现IEnumberable接口和IEnumerator接口中的方法实现的方式如下:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    class NewList<T> : IEnumerable          //实现 IEnumerable 接口的 GetEnumerator() 
    {
        private List<T> list = new List<T>();
        
        public void Add(T item)
        {
            list.Add(item);
        }
        public IEnumerator GetEnumerator()
        {
            return new NewListEnumerator<T>(this);
        }
    
        private class NewListEnumerator<T>: IEnumerator
        {
            private int position = -1;
            private NewList<T> newList;
            public NewListEnumerator(NewList<T> newList)
            {
                this.newList = newList;
            }
    
            public object Current
            {
                get 
                {
                    return newList.list[position];
                }
            }
    
            public bool MoveNext()
            {
                if(position < newList.list.Count - 1)
                {
                    position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            public void Reset()
            {
                position = -1;
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            NewList<string> newList = new NewList<string>();
            newList.Add("a");
            newList.Add("b");
            foreach(string item in newList)
            {
                Console.WriteLine("item:" + item);
            }
        }
    }
  • 相关阅读:
    Proj THUDBFuzz Paper Reading: PMFuzz: Test Case Generation for Persistent Memory Programs
    入围 WF 后训练记
    算法竞赛历程
    2021 多校 杭电 第十场
    2021 多校 杭电 第九场
    2021 多校 牛客 第十场
    2021 多校 牛客 第九场
    2021 多校 杭电 第八场
    2021 多校 杭电 第六场
    2021 多校 杭电 第七场
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/4921151.html
Copyright © 2011-2022 走看看