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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AlinSpace.Returns" Version="6.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlinSpace.Returns --version 6.0.9                
#r "nuget: AlinSpace.Returns, 6.0.9"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

NuGet version (AlinSpace.Returns)

A simple fluent library for optional results and error results.

NuGet package

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
6.0.9 876 10/12/2022
6.0.8 795 10/11/2022
6.0.5 916 2/9/2022
6.0.0 1,762 11/20/2021
5.0.1 809 7/31/2021
5.0.0 884 7/30/2021