Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

System.Collections.Generic.IAsyncEnumerable: odniesienie praktyczne

System.Collections.Generic.IAsyncEnumerable: odniesienie praktyczne: IAsyncEnumerable<T> opisuje strumień, którego kolejne MoveNextAsync mogą asynchronicznie czekać.

Wersja: .NET API; verified on .NET 10.0.14

Składnia

System.Collections.Generic.IAsyncEnumerable /* select a documented member or constructor */

IAsyncEnumerable opisuje strumień, którego kolejne MoveNextAsync mogą asynchronicznie czekać.

Stosuj do danych napływających stopniowo bez buforowania całego wyniku.

Propaguj CancellationToken i zwalniaj enumerator przez await foreach/await using.

Źródło może być jednokrotne, nieskończone lub zgłosić wyjątek dopiero podczas enumeracji.

Kompletny program rzeczywiście używa System.Collections.Generic.IAsyncEnumerable; oczekiwany stdout to dokładnie 7.

Przykłady

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;

await foreach (var value in Values()) Console.WriteLine(value);

static async IAsyncEnumerable<int> Values() { yield return 7; await Task.CompletedTask; }

Wynik

7

Kompletny program rzeczywiście używa `System.Collections.Generic.IAsyncEnumerable`; oczekiwany stdout to dokładnie `7`.

Źródła