ResultError Struct
Definition
Represents the error carried by a failed Result or Result<T>: a human-readable message, an optional machine-readable code, and an optional originating exception.
public readonly struct ResultError : IEquatable<ResultError>
- Implements
- Inherited Members
- Extension Methods
Remarks
Create errors with FromMessage(string, string?) or FromException(Exception, string?),
or lift a plain message through the implicit conversion from string. default(ResultError) is
valid and total: Message returns Empty while Code and
Exception are null.
Equality compares Code and Message ordinally and Exception by reference; the hash code is computed from Code and Message only, so two errors that differ solely by exception instance hash identically but compare unequal.
Properties
Code
Gets the optional machine-readable error code.
public string? Code { get; }
Property Value
Exception
Gets the exception the error was created from, when any.
public Exception? Exception { get; }
Property Value
- Exception
The exception passed to FromException(Exception, string?), or null when the error did not originate from an exception.
Message
Gets the human-readable error message.
public string Message { get; }
Property Value
Methods
Equals(ResultError)
Determines whether the specified error is equal to the current instance.
public bool Equals(ResultError other)
Parameters
otherResultErrorThe error to compare with this instance.
Returns
Equals(object?)
Determines whether the specified object is equal to the current error.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare. Only another ResultError can be equal; all other types, including null, return false.
Returns
- bool
true if
objis a ResultError equal to this instance; otherwise, false.
FromException(Exception, string?)
Creates an error from the specified exception and optional code.
public static ResultError FromException(Exception exception, string? code = null)
Parameters
exceptionExceptionThe originating exception. Must not be null.
codestringThe optional machine-readable code.
Returns
- ResultError
A ResultError whose Message is the exception's Message and whose Exception is the exception itself.
Exceptions
- ArgumentNullException
exceptionis null.
FromMessage(string, string?)
Creates an error from the specified message and optional code.
public static ResultError FromMessage(string message, string? code = null)
Parameters
messagestringThe human-readable error message. Must not be null.
codestringThe optional machine-readable code.
Returns
- ResultError
A ResultError carrying the message and code, with no exception.
Examples
var plain = ResultError.FromMessage("The file was not found.");
var coded = ResultError.FromMessage("The file was not found.", "IO.NotFound");
Exceptions
- ArgumentNullException
messageis null.
GetHashCode()
Returns a hash code for the current instance consistent with Equals(ResultError).
public override int GetHashCode()
Returns
ToString()
Returns a string representation of the error.
public override string ToString()
Returns
Operators
operator ==(ResultError, ResultError)
Determines whether two errors are equal.
public static bool operator ==(ResultError left, ResultError right)
Parameters
leftResultErrorThe first error to compare.
rightResultErrorThe second error to compare.
Returns
implicit operator ResultError(string)
Converts a message string to an error with no code and no exception.
public static implicit operator ResultError(string message)
Parameters
Returns
- ResultError
A ResultError carrying the message.
Remarks
The conversion delegates to FromMessage(string, string?) and therefore rejects a null string rather than producing a default error.
Exceptions
- ArgumentNullException
messageis null.
operator !=(ResultError, ResultError)
Determines whether two errors are not equal.
public static bool operator !=(ResultError left, ResultError right)
Parameters
leftResultErrorThe first error to compare.
rightResultErrorThe second error to compare.
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |