迭代器

实现一个迭代器必须实现IEnumerator
01.传统方式来实现自定义的迭代器

using System;
using System.Collections;
using System.Net.NetworkInformation;
using static System.Console;
namespace Interpreter
{
    class Program
    {
        static void Main(string[] args)
        {
            Friends friends=new Friends();
            foreach (Friend friend in friends)
            {
                WriteLine(friend.Name);
            }

            ReadKey();
        }
    }

    class Friend
    {
        public string Name { get; set; }

        public Friend(string name)
        {
            this.Name = name;
        }
    }

    class Friends:IEnumerable
    {
        private Friend[] friendArray;

        public Friends()
        {
            friendArray=new Friend[]
            {
                new Friend("张三"),
                new Friend("李四"),
                new Friend("王五"),
            };
        }

        //索引器
        public Friend this[int index]
        {
            get { return friendArray[index]; }
        }

        public int Count
        {
            get { return friendArray.Length; }
        }
        public IEnumerator GetEnumerator()
        {
            return new FriendIterator(this);
            
        }

    }

    //自定义迭代器,实现 IEnumerator接口

    class FriendIterator:IEnumerator
    {
        private readonly Friends friends;
        private int index;
        private Friend current;

        public FriendIterator(Friends friends)
        {
            this.friends = friends;
            index = 0;
        }
        public bool MoveNext()
        {
            if (index+1>friends.Count)
            {
                return false;
            }
            else
            {
                this.current = friends[index];
                index++;
                return true;
            }
        }

        public void Reset()
        {
            index = 0;
        }

        public object Current
        {
            get
            {
                return this.current;
            }
        }

    }
}

在这里插入图片描述
在这里插入图片描述
02.C#2.0的新特性简化迭代器的实现

using System;
using System.Collections;
using static System.Console;
namespace Interpreter2
{
    class Program
    {
        static void Main(string[] args)
        {
            Friends friends=new Friends();
            foreach (Friend friend in friends)
            {
                WriteLine(friend.Name);
            }
            ReadKey();
        }

    }

    class Friend
    {
        public string Name { get; set; }
        public Friend(string name)
        {
            this.Name = name;
        }
    }

    class Friends:IEnumerable
    {
        private Friend[] friendarray;
        public Friends()
        {
            friendarray = new Friend[]
            {
                new Friend("张三"),
                new Friend("李四"),
                new Friend("王五"),
            };
        }

        public Friend this[int index]
        {
            get { return friendarray[index]; }
        }
        public int Count
        {
            get { return friendarray.Length; }
        }
        public IEnumerator GetEnumerator()
        {
            for (int index = 0; index < friendarray.Length; index++)
            {
                yield return friendarray[index];
            }
        }
    }

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值