温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何实现迭代器方法

发布时间:2022-03-07 09:39:31 来源:亿速云 阅读:238 作者:小新 栏目:开发技术

这篇文章主要介绍C#如何实现迭代器方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

1.迭代器方法

可以使用foreach循环语句进行的迭代的方法,称为可迭代方法,或者迭代器方法。

迭代器用法法介绍。

  迭代器用于依次返回每个元素,一般用于foreach循环语句。迭代器方法需要使用yield return语句。

 yield return 语句介绍:

  保持代码的当前位置,在下一次调用迭代器方法时执行。

C#如何实现迭代器方法

 迭代方法在使用过程中左右步骤对应。yield return语句主要是返回一个结果作为函数调用的结果。并记录当前运行位置,当下次函数被调用时,在当前位置执行这个函数。在迭代块中除了yield return外,不允许出现普通的return语句。

迭代方法使用的命名空间为using System.Collections.Generic;

下面代码为迭代器使用的具体代码:

class Program {     public static IEnumerable<int> Fibs()     {         int f1 = 1, f2 = 2;         while (true)         {             yield return f1;             yield return f2;             f1 += f2;             f2 += f1;         }     }     static void Main(string[] args)     {         foreach (int i in Fibs())             if (i < 20)                 Console.WriteLine("{0}", i);               else                 break;         Console.ReadKey();     }  }

 IEnumerable是泛型定义的里面的int关系到你迭代对象yield return返回值的类型。如果你定义IEnumerable<int>那么你返回的值是int类型,如果你定义IEnumerable那么你的返回值是string类型以此类推。如果你想以某个条件结束方法。可以使用外面的条件如上图所示。也可以使用yield break。

class Program {     public static IEnumerable Fibs()     {         string f1 = "1", f2 = "2";         while (true)         {             yield return f1;             yield return f2;             f1 += f2;             f2 += f1;             if (f1.Length > 8)                 yield break;         }     }       static void Main(string[] args)     {         foreach (string i in Fibs())                               Console.WriteLine("{0}", i);         Console.ReadKey();     }  }

2.手动实现迭代器方法

首先是通过使用接口IEnumerable的方式,然后编写IEnumerator GetEnumerator()的方式。在代码中控制索引位置,和循环次数。如果索引位置出错则使用代码throw new NotImplementedException()报错。

using System; using System.Collections; using System.Collections.Generic;     namespace test02 {     class Program     {         static void Main(string[] args)         {             object [] e = new object[5] { 1, 2, 3, 4, 5 };             Itear01 s = new Itear01(e,2);             foreach (object i in s)                     Console.WriteLine("{0}", i);             Console.ReadKey();         }      }       public class Itear01 : IEnumerable     {         object[] values;         int StartPoint=-1;         int current=0;         public Itear01(object[] values,int StartPoint)         {             this.values = values;             this.StartPoint = StartPoint;         }         public IEnumerator GetEnumerator()         {             if(this.StartPoint==-1)                 throw new NotImplementedException();             while(true)             {                 yield return this.values[StartPoint];                 StartPoint = (StartPoint + 1) % values.Length;                 current++;                 if (current == values.Length)                 {                     break;                 }             }                       }     } }

以上是“C#如何实现迭代器方法”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI