Struct Result
Represents a result of an operation without a value, indicating success or failure.
public readonly struct Result
- Inherited Members
- Extension Methods
Properties
Failed
Gets a value indicating whether the operation failed.
public bool Failed { get; }
Property Value
Problems
Gets the collection of problems associated with the result, if any.
public ResultProblemCollection? Problems { get; }
Property Value
Succeeded
Gets a value indicating whether the operation succeeded.
public bool Succeeded { get; }
Property Value
Methods
Chain(params IEnumerable<Func<Result>>)
Executes a sequence of functions that return a Result. If any function in the chain returns a result containing problems, execution stops, and that result is returned. Otherwise, returns a successful result.
public static Result Chain(params IEnumerable<Func<Result>> links)
Parameters
linksIEnumerable<Func<Result>>An array of functions that return a Result.
Returns
- Result
The first result with problems, or a successful result if all functions succeed.
Chain<T1, T2>(Func<Result<T1>>, Func<T1, Result<T2>>)
Chains two result-producing functions, returning a combined result if both succeed.
public static Result<T2> Chain<T1, T2>(Func<Result<T1>> a, Func<T1, Result<T2>> b)
Parameters
aFunc<Result<T1>>The first function that produces a result.
bFunc<T1, Result<T2>>The second function that produces a result.
Returns
- Result<T2>
The result of the second function if both succeed; otherwise, the first encountered problem.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
Chain<T1, T2, T3>(Func<Result<T1>>, Func<T1, Result<T2>>, Func<T2, Result<T3>>)
Chains three result-producing functions, returning a combined result if all succeed.
public static Result<T3> Chain<T1, T2, T3>(Func<Result<T1>> a, Func<T1, Result<T2>> b, Func<T2, Result<T3>> c)
Parameters
aFunc<Result<T1>>The first function that produces a result.
bFunc<T1, Result<T2>>The second function that produces a result.
cFunc<T2, Result<T3>>The third function that produces a result.
Returns
- Result<T3>
The result of the third function if all succeed; otherwise, the first encountered problem.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
Chain<T1, T2, T3, T4>(Func<Result<T1>>, Func<T1, Result<T2>>, Func<T2, Result<T3>>, Func<T3, Result<T4>>)
Chains four result-producing functions, returning a combined result if all succeed.
public static Result<T4> Chain<T1, T2, T3, T4>(Func<Result<T1>> a, Func<T1, Result<T2>> b, Func<T2, Result<T3>> c, Func<T3, Result<T4>> d)
Parameters
aFunc<Result<T1>>The first function that produces a result.
bFunc<T1, Result<T2>>The second function that produces a result.
cFunc<T2, Result<T3>>The third function that produces a result.
dFunc<T3, Result<T4>>The fourth function that produces a result.
Returns
- Result<T4>
The result of the fourth function if all succeed; otherwise, the first encountered problem.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
Concat(params IEnumerable<Result>)
Concatenates a sequence of results.
public static Result Concat(params IEnumerable<Result> results)
Parameters
resultsIEnumerable<Result>The results.
Returns
- Result
A successful result if all functions succeed; otherwise, all encountered problems.
Concat(params IEnumerable<Func<Result>>)
Concatenates a sequence of functions that produce results.
public static Result Concat(params IEnumerable<Func<Result>> elements)
Parameters
elementsIEnumerable<Func<Result>>The functions that produce results.
Returns
- Result
A successful result if all functions succeed; otherwise, all encountered problems.
Concat<T1, T2>(Result<T1>, Result<T2>)
Concatenates two Result<T> values into a tuple if both succeed, or aggregates all problems if one or both fail.
public static Result<(T1, T2)> Concat<T1, T2>(Result<T1> a, Result<T2> b)
Parameters
Returns
- Result<(T1, T2)>
A Result<T> containing a tuple of the two values if both succeeded; otherwise a ResultProblemCollection containing all validation problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
Concat<T1, T2>(Func<Result<T1>>, Func<Result<T2>>)
Invokes two functions that produce Result<T> values and concatenates them into a tuple by delegating to the non‑Func Concat<T1, T2>(Result<T1>, Result<T2>) overload.
public static Result<(T1, T2)> Concat<T1, T2>(Func<Result<T1>> a, Func<Result<T2>> b)
Parameters
aFunc<Result<T1>>A function that produces the first Result<T>.
bFunc<Result<T2>>A function that produces the second Result<T>.
Returns
- Result<(T1, T2)>
A Result<T> containing a tuple of the two values if both succeeded; otherwise a ResultProblemCollection containing all aggregated problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
Concat<T1, T2, T3>(Result<T1>, Result<T2>, Result<T3>)
Concatenates three Result<T> values into a tuple if all succeed, or aggregates all problems if any fail.
public static Result<(T1, T2, T3)> Concat<T1, T2, T3>(Result<T1> a, Result<T2> b, Result<T3> c)
Parameters
aResult<T1>The first Result<T> value.
bResult<T2>The second Result<T> value.
cResult<T3>The third Result<T> value.
Returns
- Result<(T1, T2, T3)>
A Result<T> containing a tuple of the three values if all succeeded; otherwise a ResultProblemCollection containing all validation problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
Concat<T1, T2, T3>(Func<Result<T1>>, Func<Result<T2>>, Func<Result<T3>>)
Invokes three functions that produce Result<T> values and concatenates them into a tuple by delegating to the non‑Func Concat<T1, T2, T3>(Result<T1>, Result<T2>, Result<T3>) overload.
public static Result<(T1, T2, T3)> Concat<T1, T2, T3>(Func<Result<T1>> a, Func<Result<T2>> b, Func<Result<T3>> c)
Parameters
aFunc<Result<T1>>A function that produces the first Result<T>.
bFunc<Result<T2>>A function that produces the second Result<T>.
cFunc<Result<T3>>A function that produces the third Result<T>.
Returns
- Result<(T1, T2, T3)>
A Result<T> containing a tuple of the three values if all succeeded; otherwise a ResultProblemCollection containing all aggregated problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
Concat<T1, T2, T3, T4>(Result<T1>, Result<T2>, Result<T3>, Result<T4>)
Concatenates four Result<T> values into a tuple if all succeed, or aggregates all problems if any fail.
public static Result<(T1, T2, T3, T4)> Concat<T1, T2, T3, T4>(Result<T1> a, Result<T2> b, Result<T3> c, Result<T4> d)
Parameters
aResult<T1>The first Result<T> value.
bResult<T2>The second Result<T> value.
cResult<T3>The third Result<T> value.
dResult<T4>The fourth Result<T> value.
Returns
- Result<(T1, T2, T3, T4)>
A Result<T> containing a tuple of the four values if all succeeded; otherwise a ResultProblemCollection containing all validation problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
Concat<T1, T2, T3, T4>(Func<Result<T1>>, Func<Result<T2>>, Func<Result<T3>>, Func<Result<T4>>)
Invokes four functions that produce Result<T> values and concatenates them into a tuple by delegating to the non‑Func Concat<T1, T2, T3, T4>(Result<T1>, Result<T2>, Result<T3>, Result<T4>) overload.
public static Result<(T1, T2, T3, T4)> Concat<T1, T2, T3, T4>(Func<Result<T1>> a, Func<Result<T2>> b, Func<Result<T3>> c, Func<Result<T4>> d)
Parameters
aFunc<Result<T1>>A function that produces the first Result<T>.
bFunc<Result<T2>>A function that produces the second Result<T>.
cFunc<Result<T3>>A function that produces the third Result<T>.
dFunc<Result<T4>>A function that produces the fourth Result<T>.
Returns
- Result<(T1, T2, T3, T4)>
A Result<T> containing a tuple of the four values if all succeeded; otherwise a ResultProblemCollection containing all aggregated problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
Concat<T1, T2, T3, T4, T5>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>)
Concatenates five Result<T> values into a tuple if all succeed, or aggregates all problems if any fail.
public static Result<(T1, T2, T3, T4, T5)> Concat<T1, T2, T3, T4, T5>(Result<T1> a, Result<T2> b, Result<T3> c, Result<T4> d, Result<T5> e)
Parameters
aResult<T1>The first Result<T> value.
bResult<T2>The second Result<T> value.
cResult<T3>The third Result<T> value.
dResult<T4>The fourth Result<T> value.
eResult<T5>The fifth Result<T> value.
Returns
- Result<(T1, T2, T3, T4, T5)>
A Result<T> containing a tuple of the five values if all succeeded; otherwise a ResultProblemCollection containing all validation problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
T5The type of the fifth result value.
Concat<T1, T2, T3, T4, T5>(Func<Result<T1>>, Func<Result<T2>>, Func<Result<T3>>, Func<Result<T4>>, Func<Result<T5>>)
Invokes five functions that produce Result<T> values and concatenates them into a tuple by delegating to the non‑Func Concat<T1, T2, T3, T4, T5>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>) overload.
public static Result<(T1, T2, T3, T4, T5)> Concat<T1, T2, T3, T4, T5>(Func<Result<T1>> a, Func<Result<T2>> b, Func<Result<T3>> c, Func<Result<T4>> d, Func<Result<T5>> e)
Parameters
aFunc<Result<T1>>A function that produces the first Result<T>.
bFunc<Result<T2>>A function that produces the second Result<T>.
cFunc<Result<T3>>A function that produces the third Result<T>.
dFunc<Result<T4>>A function that produces the fourth Result<T>.
eFunc<Result<T5>>A function that produces the fifth Result<T>.
Returns
- Result<(T1, T2, T3, T4, T5)>
A Result<T> containing a tuple of the five values if all succeeded; otherwise a ResultProblemCollection containing all aggregated problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
T5The type of the fifth result value.
Concat<T1, T2, T3, T4, T5, T6>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>)
Concatenates six Result<T> values into a tuple if all succeed, or aggregates all problems if any fail.
public static Result<(T1, T2, T3, T4, T5, T6)> Concat<T1, T2, T3, T4, T5, T6>(Result<T1> a, Result<T2> b, Result<T3> c, Result<T4> d, Result<T5> e, Result<T6> f)
Parameters
aResult<T1>The first Result<T> value.
bResult<T2>The second Result<T> value.
cResult<T3>The third Result<T> value.
dResult<T4>The fourth Result<T> value.
eResult<T5>The fifth Result<T> value.
fResult<T6>The sixth Result<T> value.
Returns
- Result<(T1, T2, T3, T4, T5, T6)>
A Result<T> containing a tuple of the six values if all succeeded; otherwise a ResultProblemCollection containing all validation problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
T5The type of the fifth result value.
T6The type of the sixth result value.
Concat<T1, T2, T3, T4, T5, T6>(Func<Result<T1>>, Func<Result<T2>>, Func<Result<T3>>, Func<Result<T4>>, Func<Result<T5>>, Func<Result<T6>>)
Invokes six functions that produce Result<T> values and concatenates them into a tuple by delegating to the non‑Func Concat<T1, T2, T3, T4, T5, T6>(Result<T1>, Result<T2>, Result<T3>, Result<T4>, Result<T5>, Result<T6>) overload.
public static Result<(T1, T2, T3, T4, T5, T6)> Concat<T1, T2, T3, T4, T5, T6>(Func<Result<T1>> a, Func<Result<T2>> b, Func<Result<T3>> c, Func<Result<T4>> d, Func<Result<T5>> e, Func<Result<T6>> f)
Parameters
aFunc<Result<T1>>A function that produces the first Result<T>.
bFunc<Result<T2>>A function that produces the second Result<T>.
cFunc<Result<T3>>A function that produces the third Result<T>.
dFunc<Result<T4>>A function that produces the fourth Result<T>.
eFunc<Result<T5>>A function that produces the fifth Result<T>.
fFunc<Result<T6>>A function that produces the sixth Result<T>.
Returns
- Result<(T1, T2, T3, T4, T5, T6)>
A Result<T> containing a tuple of the six values if all succeeded; otherwise a ResultProblemCollection containing all aggregated problems.
Type Parameters
T1The type of the first result value.
T2The type of the second result value.
T3The type of the third result value.
T4The type of the fourth result value.
T5The type of the fifth result value.
T6The type of the sixth result value.
Failure(params IEnumerable<ResultProblem>)
Creates a result representing failure with the specified problems.
public static Result Failure(params IEnumerable<ResultProblem> problems)
Parameters
problemsIEnumerable<ResultProblem>The problems associated with the failure.
Returns
- Result
A failure result.
Failure<T>(params IEnumerable<ResultProblem>)
Creates a result representing failure with the specified problems.
public static Result<T> Failure<T>(params IEnumerable<ResultProblem> problems)
Parameters
problemsIEnumerable<ResultProblem>The problems associated with the failure
Returns
- Result<T>
A failure result.
Type Parameters
TThe success type of the failed result.
IfProblem(Action<ResultProblemCollection>)
Executes a sequence of functions that return a Result<T>.
public Result IfProblem(Action<ResultProblemCollection> action)
Parameters
actionAction<ResultProblemCollection>The function to execute.
Returns
- Result
The result of the function.
Success()
Gets a result representing success.
public static Result Success()
Returns
Success<T>(T)
Creates a result representing success with the specified value.
public static Result<T> Success<T>(T value)
Parameters
valueT
Returns
- Result<T>
Type Parameters
T
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
TryPickProblems(out ResultProblemCollection?)
Attempts to retrieve the problems associated with the result.
public bool TryPickProblems(out ResultProblemCollection? problems)
Parameters
problemsResultProblemCollectionWhen this method returns true, contains the problems. Otherwise, null.
Returns
Try<TException>(Action, string?, params object[])
Attempts to execute the specified action and returns a Result.
If an exception of type TException is thrown, it is captured
as a problem in the result.
public static Result Try<TException>(Action action, string? message = null, params object[] args) where TException : Exception
Parameters
actionActionThe action to execute.
messagestringAn optional message providing additional context if an exception is thrown. Supports composite formatting.
argsobject[]Optional arguments for formatting the message.
Returns
- Result
A successful result if no exception is thrown; otherwise, a failure result containing details of the caught exception.
Type Parameters
TExceptionThe type of exception to catch.
Try<TValue, TException>(Func<TValue>, string?, params object[])
Attempts to execute the specified function and returns a Result<T>.
If an exception of type TException is thrown, it is captured
as a problem in the result.
public static Result<TValue> Try<TValue, TException>(Func<TValue> action, string? message = null, params object[] args) where TException : Exception
Parameters
actionFunc<TValue>The function to execute.
messagestringAn optional message providing additional context if an exception is thrown. Supports composite formatting.
argsobject[]Optional arguments for formatting the message.
Returns
- Result<TValue>
A successful result containing the function's return value if no exception is thrown; otherwise, a failure result containing details of the caught exception.
Type Parameters
TValueThe type of value returned by the function.
TExceptionThe type of exception to catch.
Operators
implicit operator Result(ResultProblem)
Converts the specified problem to a failure result.
public static implicit operator Result(ResultProblem problem)
Parameters
problemResultProblemThe problem to convert.
Returns
- Result
A failure result.
implicit operator Result(ResultProblemCollection)
Converts the specified problems to a failure result.
public static implicit operator Result(ResultProblemCollection problems)
Parameters
problemsResultProblemCollectionThe problems to convert.
Returns
- Result
A failure result.