Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

asynchrony and concurrency: practical reference

asynchrony and concurrency: practical reference: Asynchrony frees a thread while waiting, while concurrency lets several operations make progress together.

Version: C# language and .NET 10 reference runtime

Syntax

// async concurrency

Asynchrony frees a thread while waiting, while concurrency lets several operations make progress together.

Use async and await for I/O and controlled primitives for shared state.

Async does not guarantee parallelism; blocking Result or Wait can deadlock a context.

Exceptions, cancellation, and partial completion of several Task instances require an explicit policy.

The complete program actually uses asynchrony and concurrency; its exact expected stdout is 5.

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;

var results = await Task.WhenAll(Task.FromResult(2), Task.Run(() => 3)); Console.WriteLine(results.Sum());

Result

5

The complete program actually uses `asynchrony and concurrency`; its exact expected stdout is `5`.

Sources