Struct Result<T>
Represents a result of an operation with a value, indicating success or failure.
public readonly struct Result<T>
Type Parameters
TThe type of the result value.
- 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
Value
Gets the value associated with the result, if any.
public T? Value { get; }
Property Value
- T
Methods
Failure(params IEnumerable<ResultProblem>)
Creates a result representing failure with the specified problems.
[Obsolete("Use Result.Failure<T> instead")]
public static Result<T> Failure(params IEnumerable<ResultProblem> problems)
Parameters
problemsIEnumerable<ResultProblem>The problems associated with the failure.
Returns
- Result<T>
A failure result.
GetValueOrDefault(T)
Gets the value of the result or throws an exception if the result is a failure.
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValueTThe default value to return if the result is a failure.
Returns
- T
The value of the result or the default value.
IfProblem(Action<ResultProblemCollection>)
Executes a sequence of functions that return a Result<T>.
public Result<T> IfProblem(Action<ResultProblemCollection> action)
Parameters
actionAction<ResultProblemCollection>The function to execute.
Returns
- Result<T>
The result of the function.
Success(T)
Creates a result representing success with the specified value.
public static Result<T> Success(T value)
Parameters
valueTThe value associated with the success.
Returns
- Result<T>
A success result.
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
TryGetValueOrDefault(out T?, T)
Attempts to retrieve the value of the result or a default value.
public bool TryGetValueOrDefault(out T? value, T defaultValue)
Parameters
valueTThe value of the result or the default value.
defaultValueTThe default value to return if the result is a failure.
Returns
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
TryPickProblems(out ResultProblemCollection?, out T?)
Attempts to retrieve the problems associated with the result.
public bool TryPickProblems(out ResultProblemCollection? problems, out T? value)
Parameters
problemsResultProblemCollectionWhen this method returns true, contains the problems. Otherwise, null.
valueTWhen this method returns false, contains the value. Otherwise, null.
Returns
TryPickValue(out T?)
Attempts to retrieve the value of the result.
public bool TryPickValue(out T? value)
Parameters
Returns
TryPickValue(out T?, out ResultProblemCollection?)
Attempts to retrieve the value of the result.
public bool TryPickValue(out T? value, out ResultProblemCollection? problems)
Parameters
valueTWhen this method returns true, contains the value. Otherwise, null.
problemsResultProblemCollectionWhen this method returns false, contains the problems. Otherwise, null.
Returns
Operators
implicit operator Result<T>(ResultProblem)
Converts the specified problem to a failure result.
public static implicit operator Result<T>(ResultProblem problem)
Parameters
problemResultProblemThe problem to convert.
Returns
- Result<T>
A failure result.
implicit operator Result<T>(ResultProblemCollection)
Converts the specified problems to a failure result.
public static implicit operator Result<T>(ResultProblemCollection problems)
Parameters
problemsResultProblemCollectionThe problems to convert.
Returns
- Result<T>
A failure result.
implicit operator Result<T>(T)
Converts the specified value to a success result.
public static implicit operator Result<T>(T value)
Parameters
valueTThe value to convert.
Returns
- Result<T>
A success result.