Struct Result
- Namespace
- ReactiveUI.Extensions.Async.Internals
- Assembly
- ReactiveUI.Extensions.dll
Represents the outcome of an operation, indicating success or failure and providing error details when applicable.
public readonly struct Result : IEquatable<Result>
- Implements
- Extension Methods
Remarks
The Result struct is used to encapsulate the result of an operation, including whether it succeeded and, if not, the exception that caused the failure. Use the Success property for successful results and Failure(Exception) to create failed results. The IsSuccess and IsFailure properties allow callers to check the operation's status before accessing error information or propagating exceptions. This struct is immutable and thread-safe.
Constructors
Result(Exception)
Initializes a new instance of the Result struct. Initializes a new instance of the Result class with the specified exception.
public Result(Exception exception)
Parameters
exceptionExceptionThe exception that represents the error condition for this result. Cannot be null.
Exceptions
- ArgumentNullException
Thrown if
exceptionis null.
Properties
Exception
Gets the exception that caused the current operation to fail, if any.
public Exception? Exception { get; }
Property Value
IsFailure
Gets a value indicating whether the operation has failed.
[MemberNotNullWhen(true, "Exception")]
public bool IsFailure { get; }
Property Value
Remarks
When true, the Exception property is guaranteed to be non-null. Use
this property to check for failure before accessing error details.
IsSuccess
Gets a value indicating whether the operation completed successfully without an exception.
[MemberNotNullWhen(false, "Exception")]
public bool IsSuccess { get; }
Property Value
Remarks
If false, the Exception property is guaranteed to be non-null,
providing details about the failure.
Success
Gets a predefined result instance that indicates a successful operation.
public static Result Success { get; }
Property Value
Methods
Equals(Result)
public bool Equals(Result other)
Parameters
otherResult
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
objobject
Returns
Failure(Exception)
Creates a failed result that encapsulates the specified exception.
public static Result Failure(Exception exception)
Parameters
exceptionExceptionThe exception that describes the failure. Cannot be null.
Returns
- Result
A result representing a failure, containing the provided exception.
GetHashCode()
public override int GetHashCode()
Returns
ToString()
Returns a string that represents the result status of the operation.
public override string ToString()
Returns
- string
A string indicating "Success" if the operation was successful; otherwise, a string in the format "Failure{exception message}" containing the associated exception message.
Remarks
This method provides a concise textual representation of the operation's outcome, which can be useful for logging or debugging purposes.
TryThrow()
Throws the associated exception if the current state represents a failure.
public void TryThrow()
Remarks
This method allows callers to propagate the stored exception when a failure has occurred. If the state does not represent a failure, no action is taken and no exception is thrown.
Operators
operator ==(Result, Result)
public static bool operator ==(Result left, Result right)
Parameters
Returns
operator !=(Result, Result)
public static bool operator !=(Result left, Result right)