SResult 1.4.0

dotnet add package SResult --version 1.4.0
                    
NuGet\Install-Package SResult -Version 1.4.0
                    
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="SResult" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SResult" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="SResult" />
                    
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 SResult --version 1.4.0
                    
#r "nuget: SResult, 1.4.0"
                    
#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 SResult@1.4.0
                    
#: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=SResult&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=SResult&version=1.4.0
                    
Install as a Cake Tool

SResult

This is an "as simple as it could" and a pure Result pattern library.

Why another result pattern?

  1. Just a boiler plate basic codes made as library for everyday use. No mvc or any bulk.
  2. It has to have a Result when success.
  3. It has to have a Failure when fail.
  4. It impossible to be both or neither.

Structure

  1. Result<TValue> For simplest use. It returns built in Reason instance when failed or TValue when succeeded.

How to return a Result from a function in most basic way.

    public static Result<int> TryParseToInteger(string value)
    {
        if (int.TryParse(value, out var val))
        {
            return val;
        }
        else
        {
            return (Failure)"The value is not any integer.";
        }
    }

How to read a Result

In continuation of the example above


var result = TryParseToInteger("101");
if(result.IsSuccess(out var value, out var failure)) Console.WriteLine($"The integer value is {value}");
else Console.WriteLine(failure.Message);

The usual approach is to throw an exception when trying to access the Value when it Failed. But that brings additional concern on how the code is written and a proper exception handling. But here we will have to check for Success or Failure to gain access to the Value or the Failure. This is to reduce the concern discussed above.

Do something before returning

public bool Handler()
{
    var result = TryParseToInteger("125")
    .OnSuccess((value) => { /* Do something on success with value */ })
    .OnFailure((failure) => { /* Do something on failure with failure */ });

    return result.IsSuccess();
}

Built in Failure Type

Initially this was not part of the library. But I end up making basic Failure class for all projects which is same 95% of the time. it usually has a string message and a failure type sort of thing. So I thought to make a part of it. So those 95% cases are covered. And free to use custom type when required.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.4.0 229 6/23/2025
1.3.3 311 3/5/2025
1.3.2 158 3/1/2025
1.3.1 196 2/27/2025
1.3.0 203 2/27/2025
1.2.2 195 2/27/2025
1.2.1 199 2/27/2025
1.2.0 200 2/27/2025
1.0.2 184 12/13/2024
1.0.1 187 12/11/2024
1.0.0 187 12/6/2024