UnsuccessfulValue<TResult> Class
Definition
Provides a way to indicate a faulted operation. This class cannot be inherited.
public sealed class UnsuccessfulValue<TResult> : ConditionalValue<TResult>
Type Parameters
TResultThe type of the return value of the operation.
- Inheritance
-
objectConditionalValue<TResult>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>