Table of Contents

Class UnsuccessfulValue<TResult>

Namespace
Cuemon
Assembly
Cuemon.Kernel.dll

Provides a way to indicate a faulted operation. This class cannot be inherited.

public sealed class UnsuccessfulValue<TResult> : ConditionalValue<TResult>

Type Parameters

TResult

The type of the return value of the operation.

Inheritance
UnsuccessfulValue<TResult>
Inherited Members

Examples

The following example demonstrates how to use UnsuccessfulValue<TResult> to represent a typed operation that failed, carrying both the exception and a default result value.

using System;
using Cuemon;

namespace Contoso.Calculations;

public sealed class UnsuccessfulValueOfTResultExample
{
    public static void Run()
    {
        ConditionalValue<int> outcome = Divide(10, 0);

        Console.WriteLine($"Succeeded: {outcome.Succeeded}");
        Console.WriteLine($"Result: {outcome.Result}");
        Console.WriteLine($"Failure: {outcome.Failure?.GetType().Name}");
    }

    private static ConditionalValue<int> Divide(int dividend, int divisor)
    {
        if (divisor == 0)
        {
            return new UnsuccessfulValue<int>(new DivideByZeroException("Cannot divide by zero."), -1);
        }

        return new SuccessfulValue<int>(dividend / divisor);
    }
}

Constructors

UnsuccessfulValue(Exception, TResult)

Initializes a new instance of the UnsuccessfulValue<TResult> class.

public UnsuccessfulValue(Exception failure, TResult result = default)

Parameters

failure Exception

The Exception that caused the faulted operation.

result TResult

The optional value of result.

UnsuccessfulValue(TResult)

Initializes a new instance of the UnsuccessfulValue<TResult> class.

public UnsuccessfulValue(TResult result = default)

Parameters

result TResult

The optional value of result.

See Also

ConditionalValue<TResult>