Result types
Result (non-generic)
Section titled “Result (non-generic)”Represents an operation that succeeds or fails with no success value.
Creating results
Section titled “Creating results”var success = Result.Success();var failure = Result.Failure(Error.NotFound("item.missing", "Item not found."));
// Shorthand factory methodsvar fail = Result.NotFound("item.missing", "Item not found.");Shorthand factories exist for all error types: Fail, Validation, NotFound, Conflict, Unauthorized, Forbidden, Unexpected.
Implicit conversion
Section titled “Implicit conversion”An Error converts implicitly to a failed Result:
Result DoWork(){ return Error.Validation("input.bad", "Invalid input."); // implicit failure}Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
IsSuccess | bool | true if the operation succeeded. |
IsFailure | bool | true if the operation failed. |
Error | Error | The error. Throws InvalidOperationException if IsSuccess. |
Result<TValue>
Section titled “Result<TValue>”Represents an operation that produces a TValue on success or an Error on failure.
Creating results
Section titled “Creating results”var success = Result<int>.Success(42);var failure = Result<int>.NotFound("num.missing", "Number not found.");Implicit conversions
Section titled “Implicit conversions”Both the value and an Error convert implicitly:
Result<int> Parse(string s) => int.TryParse(s, out var v) ? v : Error.Validation("parse.fail", "Not a number.");Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
IsSuccess | bool | true if the operation succeeded. |
IsFailure | bool | true if the operation failed. |
Value | TValue | The success value. Throws InvalidOperationException if IsFailure. |
Error | Error | The error. Throws InvalidOperationException if IsSuccess. |
Result<TValue, TError>
Section titled “Result<TValue, TError>”Same shape as Result<TValue> but with a custom error type instead of the structured Error record.
Creating results
Section titled “Creating results”var success = Result<int, string>.Success(42);var failure = Result<int, string>.Failure("something went wrong");Implicit conversions
Section titled “Implicit conversions”Result<int, string> Parse(string s) => int.TryParse(s, out var v) ? v : "Not a number.";Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
IsSuccess | bool | true if the operation succeeded. |
IsFailure | bool | true if the operation failed. |
Value | TValue | The success value. Throws if IsFailure. |
Error | TError | The error. Throws if IsSuccess. |
Equality
Section titled “Equality”All three result types implement IEquatable<T> and support == / !=. Two results are equal if they are both successes with equal values, or both failures with equal errors.