Class Result<TValue, TError>
Represents the result of an operation with a typed value and a strongly-typed error. The error type is constrained to IError, enabling exhaustive error matching when combined with ErrorsOf<T1, T2> error unions.
public sealed class Result<TValue, TError> where TError : IError
Type Parameters
TValueThe type of the success value.
TErrorThe concrete error type. Must implement IError.
- Inheritance
-
Result<TValue, TError>
- Inherited Members
- Extension Methods
Remarks
Unlike Result<TValue> which accumulates errors in a list, this type
carries exactly one concrete error. Use with Bind to grow the error union one
slot at a time, enabling compile-time exhaustive matching at the call site:
Result<Order, ValidationError> Validate(CheckoutRequest req) => ...
Result<Order, InventoryError> ReserveInventory(Order order) => ...
// Pipeline — union grows automatically
Result<Order, ErrorsOf<ValidationError, InventoryError>>
Process(CheckoutRequest req) =>
Validate(req).Bind(ReserveInventory);
Properties
Context
Ambient context carried through this pipeline — entity type, runtime identity, correlation, operation name, and tenant. Seeded automatically by Ok/Fail and propagated by all pipeline operators (parent-wins).
public ResultContext? Context { get; }
Property Value
Error
Gets the error value.
public TError Error { get; }
Property Value
- TError
Exceptions
- InvalidOperationException
Thrown when the result is a success. Check IsFailure first.
IsFailure
Gets a value indicating whether the operation failed.
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether the operation succeeded.
public bool IsSuccess { get; }
Property Value
Value
Gets the success value.
public TValue Value { get; }
Property Value
- TValue
Exceptions
- InvalidOperationException
Thrown when the result is a failure. Check IsSuccess first.
Methods
Fail(TError)
Creates a failed result with the given error.
public static Result<TValue, TError> Fail(TError error)
Parameters
errorTError
Returns
- Result<TValue, TError>
Ok(TValue)
Creates a successful result with the given value.
public static Result<TValue, TError> Ok(TValue value)
Parameters
valueTValue
Returns
- Result<TValue, TError>
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
WithContext(string?, string?, string?, string?)
Returns a copy of this result with additional context values merged in. Only non-null arguments overwrite existing context fields.
public Result<TValue, TError> WithContext(string? entityId = null, string? correlationId = null, string? operationName = null, string? tenantId = null)
Parameters
Returns
- Result<TValue, TError>