Skip to content

Result types

Represents an operation that succeeds or fails with no success value.

var success = Result.Success();
var failure = Result.Failure(Error.NotFound("item.missing", "Item not found."));
// Shorthand factory methods
var fail = Result.NotFound("item.missing", "Item not found.");

Shorthand factories exist for all error types: Fail, Validation, NotFound, Conflict, Unauthorized, Forbidden, Unexpected.

An Error converts implicitly to a failed Result:

Result DoWork()
{
return Error.Validation("input.bad", "Invalid input."); // implicit failure
}
PropertyTypeDescription
IsSuccessbooltrue if the operation succeeded.
IsFailurebooltrue if the operation failed.
ErrorErrorThe error. Throws InvalidOperationException if IsSuccess.

Represents an operation that produces a TValue on success or an Error on failure.

var success = Result<int>.Success(42);
var failure = Result<int>.NotFound("num.missing", "Number not found.");

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.");
PropertyTypeDescription
IsSuccessbooltrue if the operation succeeded.
IsFailurebooltrue if the operation failed.
ValueTValueThe success value. Throws InvalidOperationException if IsFailure.
ErrorErrorThe error. Throws InvalidOperationException if IsSuccess.

Same shape as Result<TValue> but with a custom error type instead of the structured Error record.

var success = Result<int, string>.Success(42);
var failure = Result<int, string>.Failure("something went wrong");
Result<int, string> Parse(string s) =>
int.TryParse(s, out var v) ? v : "Not a number.";
PropertyTypeDescription
IsSuccessbooltrue if the operation succeeded.
IsFailurebooltrue if the operation failed.
ValueTValueThe success value. Throws if IsFailure.
ErrorTErrorThe error. Throws if IsSuccess.

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.