Result<T>
Class public
Encapsulates the outcome of a domain operation as either a success carrying a value of type
T, or a failure carrying an error message. Definition
Section titled Definitionpublic class Result<T>T Type Parameters
Section titled Type ParametersT The type of the value returned on success.
Remarks
Section titled RemarksResult`1 implements the discriminated-result pattern: callers inspect IsSuccess (or IsFailure) before accessing Value or Error. Accessing Value on a failed result returns default for T. Prefer the static factory methods Ok(`0) and String) over direct construction to ensure the success/error invariants are satisfied. This type can be used as a method return value in service layers to avoid throwing exceptions for expected failure conditions.Properties4
Section titled Propertiesstring? string? Result<T>.ErrorGets the error message describing why the operation failed.
IsFailure Section titled IsFailure bool bool Result<T>.IsFailureGets a value indicating whether the operation failed.
IsSuccess Section titled IsSuccess bool bool Result<T>.IsSuccessGets a value indicating whether the operation succeeded.
T? T? Result<T>.ValueGets the value produced by a successful operation.
Methods2
Section titled MethodsResult<T> Result<T>.Fail( string error)Creates a failed result with the specified error message.
Parameters
error string A message describing the failure.
Returns
Result<T> A
Result`1 representing a failed outcome. Examples
Section titled ExamplesUsing Result to handle success and failure paths:
Result<Customer> result = await customerService.TryFindAsync(id);
if (result.IsSuccess){ Console.WriteLine($"Found: {result.Value!.FullName}");}else{ Console.WriteLine($"Error: {result.Error}");}Returning a Result from a service method:
public Result<Customer> Validate(Customer customer){ if (string.IsNullOrWhiteSpace(customer.FullName)) return Result<Customer>.Fail("Full name is required.");
return Result<Customer>.Ok(customer);}