C# 非泛型集合的简单迭代

IEnumerator接口:支持对非泛型集合的简单迭代,使得foreach可以遍寻集合

 

using System;
using System.Collections;
复制代码

 

复制代码
    public class Family
    {
        private string husban = null;
        public string Husban
        {
            get { return husban; }
            set { husban = value; }
        }

        private string wife = null;
        public string Wife
        {
            get { return wife; }
            set { this.wife = value; }
        }

        private string son = null;
        public string Son
        {
            get { return this.son; }
            set { this.son = value; }
        }

        private string daughter = null;
        public string Daughter
        {
            get { return this.daughter; }
            set { this.daughter = value; }
        }

        public Family(string husban, string wife, string daughter, string son)
        {
            this.husban = husban;
            this.wife = wife;
            this.daughter = daughter;
            this.son = son;
        }
    }

    public class Families : IEnumerable
    {
        Family[] families;
        public Families(Family[] familyList)
        {
            this.families = new Family[familyList.Length];
            for (int i = 0; i < familyList.Length; i++)
                this.families[i] = familyList[i];
        }

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


        FamilyEnumerator GetEnumerator()
        {
            return new FamilyEnumerator(this.families);
        }
    }

    public class FamilyEnumerator : IEnumerator
    {
        public Family[] families;
        int index = -1;
        public FamilyEnumerator(Family[] familyList)
        {
            this.families = familyList;
        }

        /// <summary>
        /// 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前
        /// </summary>
        public void Reset()
        {
            index = -1;
        }


        /// <summary>
        /// 获取集合中位于枚举数当前位置的元素
        /// </summary>
        public Family Current
        {
            get
            {
                try
                {
                    return this.families[index];
                }
                catch (IndexOutOfRangeException e)
                {
                    throw new InvalidOperationException();
                }
            }
        }


        /// <summary>
        /// 将枚举数推进到集合的下一个元素
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            index++;
            return index < this.families.Length;
        }


        object IEnumerator.Current
        {
            get
            {
                return Current;
            }
        }
        
    }
复制代码

 

复制代码
复制代码
        static void Main(string[] args)
        {
           
            Family[] familyList = new Family[]
                {
                     new Family("echo","zp","cc","ef"),
                     new Family("zhubaba","zhumama","peiqi","qiaozhi"),
                };

            Families families = new Families(familyList);
            foreach (Family fam in families)
            {
                Console.WriteLine($"{fam.Husban}  {fam.Wife}  {fam.Son}  {fam.Daughter}");
            }
        }
复制代码

 

 

posted @   echo-efun  阅读(114)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示