Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Threading.Tasks.ValueTask: practical reference

System.Threading.Tasks.ValueTask: practical reference: ValueTask<T> can wrap a synchronous result or an asynchronous operation with fewer allocations.

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

Syntax

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

ValueTask can wrap a synchronous result or an asynchronous operation with fewer allocations.

Use it in measured APIs that complete synchronously often; normally await it immediately.

Do not await it repeatedly or compose it without AsTask unless the source supports that.

A default ValueTask is complete; boxing or AsTask can erase the allocation saving.

The complete program actually uses System.Threading.Tasks.ValueTask; its exact expected stdout is 10.

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;

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

Result

10

The complete program actually uses `System.Threading.Tasks.ValueTask`; its exact expected stdout is `10`.

Sources