CKTIS / C#
yield: practical reference
yield: practical reference: yield return produces the next iterator element, while yield break ends the sequence.
Version: C# language; verified against C# 14 reference
Syntax
yield /* grammar-specific form */yield return produces the next iterator element, while yield break ends the sequence.
Use it for lazy production without constructing the complete collection.
The body runs during enumeration and can run repeatedly for multiple enumerations.
Its placement in catch and finally is restricted; Dispose triggers iterator cleanup.
The complete program actually uses yield; its exact expected stdout is 1,2.
Examples
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(",", Values())); static IEnumerable<int> Values() { yield return 1; yield return 2; }
Result
1,2
The complete program actually uses `yield`; its exact expected stdout is `1,2`.