CKTIS / C#
iterators and generators: riferimento pratico
iterators and generators: riferimento pratico: Un iteratore produce elementi gradualmente tramite yield e conserva lo stato tra le chiamate MoveNext.
Versione: C# language and .NET 10 reference runtime
Sintassi
// iteratorsUn iteratore produce elementi gradualmente tramite yield e conserva lo stato tra le chiamate MoveNext.
Usalo per sequenze lazy senza costruire una raccolta completa.
Il codice viene eseguito durante l’enumerazione, non alla chiamata del metodo.
Dispose esegue il finally dell’iteratore; una sorgente infinita richiede limite o annullamento.
Il programma completo usa realmente iterators and generators; lo stdout esatto atteso è 0,1,2.
Esempi
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;
Console.WriteLine(string.Join(",", Count(3))); static IEnumerable<int> Count(int length) { for (var i = 0; i < length; i++) yield return i; }
Risultato
0,1,2
Il programma completo usa realmente `iterators and generators`; lo stdout esatto atteso è `0,1,2`.