Table of Contents

Class TaskExtensions

Namespace
Savvyio
Assembly
Savvyio.Core.dll

Extension methods for the Task<TResult> class.

public static class TaskExtensions
Inheritance
TaskExtensions

Examples

This example shows how to await a task that returns a sequence and keep only the single matching result.

using System.Collections.Generic;
using System.Threading.Tasks;
using Savvyio;

namespace ExampleApp;

public sealed class TaskExtensionsExample
{
    public Task<OrderProjection?> LoadAsync()
    {
        Task<IEnumerable<OrderProjection>> query = Task.FromResult<IEnumerable<OrderProjection>>(new[] { new OrderProjection("ORD-42") });
        return query.SingleOrDefaultAsync();
    }
}

public sealed record OrderProjection(string OrderId);

Methods

SingleOrDefaultAsync<T>(Task<IEnumerable<T>>)

Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

public static Task<T> SingleOrDefaultAsync<T>(this Task<IEnumerable<T>> source)

Parameters

source Task<IEnumerable<T>>

The Task<TResult> to return the single element of.

Returns

Task<T>

A task that represents the asynchronous operation. The task result contains the single element of the input sequence, or default (TSource) if the sequence contains no elements.

Type Parameters

T

The type of the elements of source.