Class Result
- Namespace
- Compendium.Core.Results
- Assembly
- Compendium.Core.dll
Represents the result of an operation that can either succeed or fail. Provides a functional approach to error handling without exceptions.
public class Result
- Inheritance
-
Result
- Derived
-
Result<TValue>
- Inherited Members
- Extension Methods
Constructors
Result(bool, Error)
Initializes a new instance of the Result class.
protected Result(bool isSuccess, Error error)
Parameters
isSuccessboolA value indicating whether the operation was successful.
errorErrorThe error that occurred, if any.
Properties
Error
Gets the error that occurred during the operation.
public Error Error { get; }
Property Value
IsFailure
Gets a value indicating whether the operation failed.
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether the operation was successful.
public bool IsSuccess { get; }
Property Value
Methods
Combine(params Result[])
Combines multiple results into a single result.
public static Result Combine(params Result[] results)
Parameters
resultsResult[]The results to combine.
Returns
- Result
A successful result if all results are successful; otherwise, a failed result with the first error.
Combine(IEnumerable<Result>)
Combines multiple results into a single result.
public static Result Combine(IEnumerable<Result> results)
Parameters
resultsIEnumerable<Result>The results to combine.
Returns
- Result
A successful result if all results are successful; otherwise, a failed result with the first error.
Create(bool, Error)
Creates a result based on a condition.
public static Result Create(bool condition, Error error)
Parameters
Returns
- Result
A successful result if the condition is true; otherwise, a failed result.
Create<TValue>(TValue?, Error)
Creates a result based on a nullable value condition.
public static Result<TValue> Create<TValue>(TValue? value, Error error) where TValue : struct
Parameters
valueTValue?The nullable value.
errorErrorThe error to use if the value is null.
Returns
- Result<TValue>
A successful result with the value if it has a value; otherwise, a failed result.
Type Parameters
TValueThe type of the value.
Create<TValue>(TValue?, Error)
Creates a result based on a value condition.
public static Result<TValue> Create<TValue>(TValue? value, Error error) where TValue : class
Parameters
valueTValueThe value.
errorErrorThe error to use if the value is null.
Returns
- Result<TValue>
A successful result with the value if it's not null; otherwise, a failed result.
Type Parameters
TValueThe type of the value.
Failure(Error)
Creates a failed result.
public static Result Failure(Error error)
Parameters
errorErrorThe error that occurred.
Returns
- Result
A failed result.
Failure<TValue>(Error)
Creates a failed result with a value type.
public static Result<TValue> Failure<TValue>(Error error)
Parameters
errorErrorThe error that occurred.
Returns
- Result<TValue>
A failed result.
Type Parameters
TValueThe type of the value.
Success()
Creates a successful result.
public static Result Success()
Returns
- Result
A successful result.
Success<TValue>(TValue)
Creates a successful result with a value.
public static Result<TValue> Success<TValue>(TValue value)
Parameters
valueTValueThe value.
Returns
- Result<TValue>
A successful result with the specified value.
Type Parameters
TValueThe type of the value.
ToString()
Returns a string representation of the result.
public override string ToString()
Returns
- string
A string that represents the current result.
Operators
implicit operator Result(Error)
Implicitly converts an Error to a Result.
public static implicit operator Result(Error error)
Parameters
errorErrorThe error.