DotNetElements.Core.Result 0.1.9

Prefix Reserved
dotnet add package DotNetElements.Core.Result --version 0.1.9
                    
NuGet\Install-Package DotNetElements.Core.Result -Version 0.1.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="DotNetElements.Core.Result" Version="0.1.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DotNetElements.Core.Result" Version="0.1.9" />
                    
Directory.Packages.props
<PackageReference Include="DotNetElements.Core.Result" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DotNetElements.Core.Result --version 0.1.9
                    
#r "nuget: DotNetElements.Core.Result, 0.1.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.
#:package DotNetElements.Core.Result@0.1.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DotNetElements.Core.Result&version=0.1.9
                    
Install as a Cake Addin
#tool nuget:?package=DotNetElements.Core.Result&version=0.1.9
                    
Install as a Cake Tool

About

This project provides simple Result and Result<TValue> types to be used as return types of functions as an alternative to exceptions.

The default types have a .IsOk and .IsFail property to check wether the function returned success or failure. In case of a Result<TValue> the result contains the return value of the function.

To include error state in the Result, one has to implement a custom ErrorResult<TValue, TError>. A source generator automatically provides the class implementation.

There are helper functions like Fail(), Ok() or TryGetValue(out string? resultValue) to make working with Results as simple as possible.

  1. Install nuget package > dotnet add package DotNetElements.Core.Result --version <insert-latest-version-here>

  2. Add the following to a GlobalUsing.cs

global using static DotNetElements.Core.ResultObject.ResultHelper;

Usage examples for basic Result and Result<TValue> types

Example function using a Result as return type

static Result<string> GetResultFunction(bool expectedResult)
{
    // With logging
    if (!expectedResult)
        return Fail(() => Console.WriteLine("Fail from method"));

    // Without logging
    if (!expectedResult)
        return Fail();

    return "Success from method";
}

Usage example using the .TryGetValue semantic

if (GetResultFunction(true).TryGetValue(out string? resultValue))
{
    Console.WriteLine($"Success from caller with value <{resultValue}>");
}
else
{
    Console.WriteLine("Fail from caller";
}

Usage example using manual check and GetValueUnsafe()

Result<string> result = GetResultFunction(false);

if (result.IsOk)
{
    Console.WriteLine($"Success from caller with value <{result.GetValueUnsafe()}>");
}
else
{
    Console.WriteLine("Fail from caller");
}

Usage example using explicit conversions to prevent data loss for Result with value

Result resultWithoutValue = result.AsResult();

Usage examples for source generated custom ErrorResult<TError> and ErrorResult<TValue, TError> types

Define a custom ErrorResult

namespace MyNamespace;

public enum CrudError
{
  InternalError,
  NotFound,
  DuplicateEntry
}

[ErrorResult<CrudError>]
public partial class CrudResult<TValue>;

Example function using a custom CrudResult as return type

// This is needed to enable the usage of Ok() and Fail methods
// Can be in each file or in a GlobalUsing.cs
using static MyNamespace.CrudResultHelper;

static CrudResult<string> GetCrudResultFunction(bool expectedResult)
{
    // With logging
    if (!expectedResult)
        return Fail(CrudError.InternalError, () => Console.WriteLine("Fail from method"));

    // Without logging
    if (!expectedResult)
        return Fail(CrudError.InternalError);

    return "Success from method";
}

Usage example using the .TryGetValue semantic

if (GetCrudResultFunction(true).TryGetValue(out string? crudValue, out CrudError? error))
{
    Console.WriteLine($"Success from caller with value <{crudValue}>");
}
else
{
    Console.WriteLine($"Fail from caller with error <{error}>");
}

Usage example using manual check and GetValueUnsafe()

CrudResult<string> crudResult = GetCrudResultFunction(false);

if (crudResult.IsOk)
{
    Console.WriteLine($"Success from caller with value <{crudResult.GetValueUnsafe()}>");
}
else
{
    Console.WriteLine($"Fail from caller with error <{crudResult.GetErrorUnsafe()}>");
}

Usage example using explicit conversions to prevent data loss

CrudResult crudResultWithoutValue = crudResult.AsCrudResult();
Result<string> simpleResultWithValue = crudResult.AsResultWithValue();
Result simpleResultWithoutValue = crudResult.AsResult();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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
0.1.9 113 2/8/2026
0.1.8 112 2/8/2026
0.1.7 113 2/8/2026
0.1.6 112 2/8/2026
0.1.5 198 5/22/2025
0.1.4 254 3/8/2025
0.1.3 284 3/7/2025
0.1.2 165 3/2/2025
0.1.1 177 3/2/2025
0.1.0 162 3/2/2025
0.0.9 173 11/4/2024
0.0.8 164 11/4/2024
0.0.7 159 11/4/2024
0.0.6 156 11/4/2024
0.0.4 180 11/4/2024
0.0.3 138 11/4/2024
0.0.2 159 11/4/2024