OpenResult 1.0.0
dotnet add package OpenResult --version 1.0.0
NuGet\Install-Package OpenResult -Version 1.0.0
<PackageReference Include="OpenResult" Version="1.0.0" />
<PackageVersion Include="OpenResult" Version="1.0.0" />
<PackageReference Include="OpenResult" />
paket add OpenResult --version 1.0.0
#r "nuget: OpenResult, 1.0.0"
#:package OpenResult@1.0.0
#addin nuget:?package=OpenResult&version=1.0.0
#tool nuget:?package=OpenResult&version=1.0.0
OpenResult for C#
Dead-simple, dependency-free Result pattern for .NET and C#.
API
Result
Represents the outcome of an operation—either success or failure.
Properties:
- IsSuccess:
true
if the operation succeeded. - IsFailure:
true
if the operation failed. - Error?: The associated
Error
object if failed, ornull
if successful.
Methods:
- Succeeded(): Returns
true
if successful (same asIsSuccess
). - Failed(): Returns
true
if failed (same asIsFailure
). - Failed(out Error? error): Returns
true
if failed, and outputs the associatedError
object.
Static Methods:
- Success(): Creates a successful result.
- Success(TValue value): Creates a successful result for
Result<TValue>
. <br/>Sugar syntax for result creation with a value—type is inferred from the value. - Failure(Error error): Creates a failed result with an associated error.
- Failure
<TValue>
(Error error): Creates a failed result forResult<TValue>
.
Type Inference in C#
C# allows for sugar syntax like Result.Success(value)
because it can deduce the type from the value provided.
However, it cannot infer the type for failures from just the error object.
You must specify the type explicitly, for example:
Result<int>.Failure(error);
// or
Result.Failure<int>(error);
Workaround for Clean Calls
If you want to avoid repeating the type at every failure, you can use a local helper method:
private Result<MyType> Fail(Error error) => Result<MyType>.Failure(error); // <-- Helper
// Usage:
if (shouldFail) return Fail(new Error("failed!"));
return Result.Success(0);
This is a C# language limitation, not a library design issue. C# cannot automatically infer the return type from the consuming method's signature—it can only do so when a value is provided.
This will not compile:
private Result<int> MyMethod()
{
//...
return Result.Failure(new Error("failed!")); // Compilation error!
// Result.Failure returns a non-generic Result, not a Result<int>.
// ---
// Correct usage:
return Result<int>.Failure(new Error("failed!"));
// or
return Result.Failure<int>(new Error("failed!"));
}
Result<TValue> (Generic Version)
All of the above, plus:
Property:
- Value: The value produced by a successful operation, or
default
if failed.
Method:
- Succeeded(out TValue? value): Returns
true
if successful, and outputs the value.
Error
Describes an error condition in a structured, chainable way.
Properties:
- Message: Human-readable error message.
- Code: Optional code/identifier for programmatic handling.
- Exception: Optional
Exception
instance if error was caused by an exception. - InnerError: Optional reference to another
Error
that caused this error. - Root: The deepest, original error in a chain of errors.
Methods:
- IsExceptional(): Returns
true
if this error wraps anException
. - IsExceptional(out Exception? exception): Returns
true
and outputs the wrapped exception if present.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.0 | 146 | 5/21/2025 |