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);
            }
        }
    }
  • 相关阅读:
    Spring Boot重定向的使用方法
    Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
    Jmeter性能测试之分布式(五)
    Jmeter性能测试之Monitor监控(四)
    Jmeter性能测试之关联(三)
    Jmeter性能测试之参数化(二)
    JVM 内存溢出(转载~)
    MySql workbeach 更改侧边栏大小
    Mybati example generatorConfig.xml 配置详解
    Rvm 进行gem安装时必须输入密码Your user account isn't allowed to install to the system RubyGems 解决方案
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/4921151.html
Copyright © 2011-2022 走看看