OptionSharp 0.3.1

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

OptionSharp

Another option for options in C#

Disclaimer: If you are looking for a stable, production ready lib you are better off using something like LanguageExt.Core.

Motivation

This library is primarily for myself to learn about functional programming but also takes a different opinion/approach compared to other more well established libs.

  • Focuses only on Option and Result types
  • Does not allow for null
  • Uses records as discriminated unions which is as close as you can get in c# whilst still being idiomatic (AFAIK)
  • Rust-like (ish) feel, promotes the use of strongly typed errors
  • Source generated code to keep it readable

Usage

Define your error types as below which will generate some constructor methods for convenience

[GenerateResult("Handle")]
public abstract record HandleError;

public sealed record FailedToDeserialize(string Message) : HandleError;
public sealed record InvalidId(int Id) : HandleError;

Return options and results in your domain

public Result<int, HandleError> TryGetIdOnlyIfSeven(int id) => 
        id == 7 
            ? Handle.Ok(id) 
            : Handle.Err<int>(new InvalidId(id));

Use extension methods to operate on values in a functional way

var eight = TryGetIdOnlyIfSeven(7)
    .Inspect(seven => Console.WriteLine($"seven is {seven}"))
    .Map(seven => seven + 1)
    .UnwrapOrDefault(0);
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
0.3.1 224 4/30/2025
0.2.5 210 8/24/2024
0.2.4 178 6/17/2024
0.2.1 169 5/10/2024
0.1.3-alpha 149 2/25/2024
0.1.0-alpha 145 2/11/2024