CKTIS / C#
iterators and generators: praktická reference
iterators and generators: praktická reference: Iterátor vytváří prvky postupně pomocí yield a zachovává stav mezi voláními MoveNext.
Verze: C# language and .NET 10 reference runtime
Syntaxe
// iteratorsIterátor vytváří prvky postupně pomocí yield a zachovává stav mezi voláními MoveNext.
Použijte pro líné posloupnosti bez vytváření celé kolekce.
Kód se spouští při enumeraci, nikoli při volání metody.
Dispose spustí finally iterátoru; nekonečný zdroj potřebuje limit nebo zrušení.
Úplný program skutečně používá iterators and generators; přesně očekávaný stdout je 0,1,2.
Příklady
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; }
Výsledek
0,1,2
Úplný program skutečně používá `iterators and generators`; přesně očekávaný stdout je `0,1,2`.