AlinSpace.Returns
6.0.9
Prefix Reserved
dotnet add package AlinSpace.Returns --version 6.0.9
NuGet\Install-Package AlinSpace.Returns -Version 6.0.9
<PackageReference Include="AlinSpace.Returns" Version="6.0.9" />
paket add AlinSpace.Returns --version 6.0.9
#r "nuget: AlinSpace.Returns, 6.0.9"
// Install AlinSpace.Returns as a Cake Addin #addin nuget:?package=AlinSpace.Returns&version=6.0.9 // Install AlinSpace.Returns as a Cake Tool #tool nuget:?package=AlinSpace.Returns&version=6.0.9
AlinSpace.Returns
A simple fluent library for optional results and error results.
Why?
Returning a null (or default) value on failure is not the best way of conveying the reason for the specific failure. Throwing an exception is a better way of doing it. Catching and handling exception needs a lot of boiler plate code and can also easily be forgotten to be catched. Exceptions also makes the flow of the software much more complex than return values.
Examples
Return optional value
The following method returns an optional result of type string:
Result<string> Method()
{
...
if (failure)
{
return Result<string>.None();
}
...
return data.ToResult();
}
// Create results with no value.
var result1 = new Result<string>();
var result2 = Result<string>.None();
// This will throw an exception, because there is no value.
result1.Value;
result2.Value;
// This will return false.
result1.HasValue;
result2.HasValue;
// Create result with a value.
var result3 = new Result<string>("Data");
// This will return "Data".
result3.Value;
// This will return true.
result3.HasValue;
Return result value with error value
The result can also contain an error value instead of simply beeing empty. The following method returns a result with :
Result<string, int> Method()
{
if (failure < 0)
{
return Result<string, int>.Error(failure);
}
...
return Result<string, int>.Return(data);
}
// Create result with string value and int error value.
var result1 = Result<string, int>.Error(5);
// This will throw an exception, because there is no value.
result1.Value;
// This will return false.
result1.HasValue;
result2.HasValue;
// Create result with a value.
var result3 = new Result<string>("Data");
// This will return "Data".
result3.Value;
// This will return true.
result3.HasValue;
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.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.