Skip to content

Instantly share code, notes, and snippets.

Created January 14, 2023 11:15
What would you like to do?
A nullable aware Attempt type for wrapping nullable async responses.
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// A wrapper for nullable values that correctly handles the return type based on the result.
/// </summary>
/// <typeparam name="T">The type of nullable value.</typeparam>
public readonly struct Attempt<T>
/// <summary>
/// Gets a value indicating whether the attempted return was successful.
/// </summary>
[MemberNotNullWhen(returnValue: true, member: nameof(Value))]
public bool Success => this.Value is not null;
/// <summary>
/// Gets the value when the attempted return is successful; otherwise, the default value for the type.
/// </summary>
public T? Value { get; init; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment