Znalostní bázeCentrum zkoušek
CSČeština
Vybrat jazyk13

CKTIS / C#

System.Threading.Tasks.ValueTask: praktická reference

System.Threading.Tasks.ValueTask: praktická reference: ValueTask<T> může zabalit synchronní výsledek nebo asynchronní operaci s méně alokacemi.

Verze: .NET API; verified against .NET 10 reference surface

Syntaxe

System.Threading.Tasks.ValueTask /* select a documented member or constructor */

ValueTask může zabalit synchronní výsledek nebo asynchronní operaci s méně alokacemi.

Použijte v měřených API s častým synchronním dokončením a obvykle ihned awaitujte.

Neawaitujte jej opakovaně ani neskládejte bez AsTask, pokud to zdroj nepodporuje.

Default ValueTask je dokončený; boxing nebo AsTask může úsporu alokace odstranit.

Úplný program skutečně používá System.Threading.Tasks.ValueTask; přesně očekávaný stdout je 10.

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;

ValueTask<int> task = ValueTask.FromResult(10);
Console.WriteLine(await task);

Výsledek

10

Úplný program skutečně používá `System.Threading.Tasks.ValueTask`; přesně očekávaný stdout je `10`.

Zdroje