3-8 C#中的迭代器使用

一.迭代器

 class MyList : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
    
                yield return 1;
                yield return 2;
                yield return 3;
                yield return 4;
                yield return 8;
                yield return 6;//进行六次值返回
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

 使用方法:

            var e = list.GetEnumerator();
            while(e.MoveNext())
            {
            需要实现功能的代码
            }

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;//使用IEnumerable一定要添加

namespace ConsoleApp1
{

    class A { public int a; }
    class MyList : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {
    
                yield return 1;
                yield return 2;
                yield return 3;
                yield return 4;
                yield return 8;
                yield return 6;
        
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

    class Program
    {
    static int MyCount(MyList list)
    { 
    int sum = 0;

            var e = list.GetEnumerator();
            //foreach (var i in list)
            while(e.MoveNext())
            { 
            sum++;
            }
            return sum;
    }
        static int MyMax(MyList list)
        {
            int max = int.MinValue;
            var e = list.GetEnumerator();
            while (e.MoveNext())
            {
                if (e.Current > max)
                {
                    max = e.Current;
                }
            }

            return max;
        }
            static void Main(string[] args)
        {

           var list = new MyList();
            Console.WriteLine(MyMax(list));//打印8
            Console.WriteLine(MyCount(list));//打印6

        }


    }

}

二.扩展方法

可以减少在主函数中额外使用函数

扩展空间函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    static class MyExMethod
    {
        public static int MyCount(this MyList list)
        {
            int sum = 0;
            var e = list.GetEnumerator();//枚举数
            //foreach (var i in list)
            while (e.MoveNext())
            {
                sum++;
            }
            return sum;
        }

        public static int ToInt(this String s)
        { 
        return int.Parse(s);
        }

    }
}

主空间

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;//使用IEnumerable一定要添加

namespace ConsoleApp1
{

    class A { public int a; }
    class MyList : IEnumerable<int>
    {
        public IEnumerator<int> GetEnumerator()
        {

            yield return 1;
            yield return 2;
            yield return 3;
            yield return 4;
            yield return 8;
            yield return 6;

        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
        public int MyCount(IEnumerable<int> list)
        {
            int sum = 0;

            var e = list.GetEnumerator();
            //foreach (var i in list)
            while (e.MoveNext())
            {
                sum++;
            }
            return sum;
        }
        class MyList2 : IEnumerable<int>
        {
            public IEnumerator<int> GetEnumerator()
            {

                yield return 1;
                yield return 2;
                yield return 3;
                yield return 4;
                yield return 8;
                yield return 10;

            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
            public int MyCount(IEnumerable<int> list)
            {
                int sum = 0;

                var e = list.GetEnumerator();
                //foreach (var i in list)
                while (e.MoveNext())
                {
                    sum++;
                }
                return sum;
            }

            class Program
            {


                static int MyMax(MyList list)
                {
                    int max = int.MinValue;
                    var e = list.GetEnumerator();
                    while (e.MoveNext())
                    {
                        if (e.Current > max)
                        {
                            max = e.Current;
                        }
                    }

                    return max;
                }
                static void Main(string[] args)
                {

                    var list = new MyList();
                    var list2 = new MyList2();
                    Console.WriteLine(MyMax(list));//打印8
                    Console.WriteLine(list2.MyCount(list2));//打印6

                    string a="123";
                    int b = a.ToInt();
                    Console.WriteLine(b);

                }
            }



        }

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉小萌新

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值