Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement iterator method in C #

Shulou Source: shulou.com Published: 2022-06-01 07:11:14 09月10日 Update

This article mainly introduces how to implement the iterator method in C#. It is very detailed and has a certain reference value. Friends who are interested must read it!

1. Iterator method

An iterative method that can be performed using a foreach loop statement is called an iterable method, or an iterator method.

Introduction to the usage of iterators.

Iterators are used to return each element in turn, typically for foreach loop statements. The iterator method requires a yield return statement.

The yield return statement describes:

Maintains the current position of the code and is executed the next time the iterator method is called.

The iterative method corresponds to the left and right steps in the process of use. The yield return statement mainly returns a result as the result of a function call. And record the current running location, and execute the function at the current location the next time the function is called. Ordinary return statements are not allowed in an iterative block other than yield return.

The namespace used by the iterative method is using System.Collections.Generic

The following code is the specific code used by the iterator:

Class Program {public static IEnumerable 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类型,如果你定义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. Manually implement the iterator method

First by using the interface IEnumerable, and then by writing IEnumerator GetEnumerator (). Control the index position and the number of loops in the code. If the index location is wrong, the code throw new NotImplementedException () is used to report the error.

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 (eMagne2); 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;}} above is all the content of the article "how to implement iterator methods in C #". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Iterations methods statements code location functions types loops caches content methods conditions articles indexes results normal and so on above center-left value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei MariaDB Apple MySQL