Table of Contents

UnsuccessfulValue<TResult> Class

Definition

Namespace
Cuemon
Assemblies
Cuemon.Kernel.dll
Source
src/Cuemon.Kernel/UnsuccessfulValue.cs

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
object
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

Name Description
UnsuccessfulValue(Exception, TResult)

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

UnsuccessfulValue(TResult)

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

See Also

ConditionalValue<TResult>