Fox.OptionKit 1.0.0

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

Fox.OptionKit

A minimalist, dependency-free Option<T> implementation for C#. Fox.OptionKit is a lightweight, clean, and well-documented solution for type-safe handling of optional values.

Key Features

  • Dependency-free - No external dependencies
  • Lightweight - Minimal overhead
  • Type-safe - Type-safe alternative to null values
  • Well-documented - Comprehensive XML documentation
  • Functional - Map, Bind, Match operations
  • .NET 8+ compatible - Modern .NET support

Installation

dotnet add package Fox.OptionKit

Basic Usage

Some and None

using Fox.OptionKit;

// Create an option with a value
var some = Option.Some(42);
Console.WriteLine(some.HasValue); // True
Console.WriteLine(some.Value);    // 42

// Create an empty option
var none = Option.None<int>();
Console.WriteLine(none.IsNone);   // True

ValueOr - Safe Value Access

var value1 = Option.Some(42).ValueOr(0);    // 42
var value2 = Option.None<int>().ValueOr(0); // 0

Map - Value Transformation

var result = Option.Some(10)
    .Map(x => x * 2)
    .Map(x => x + 5)
    .Map(x => $"Result: {x}");

Console.WriteLine(result); // Some(Result: 25)

Bind - Monadic Composition

Option<int> Divide(int numerator, int denominator)
{
    return denominator == 0 
        ? Option.None<int>() 
        : Option.Some(numerator / denominator);
}

var result = Option.Some(100)
    .Bind(x => Divide(x, 2))
    .Bind(x => Divide(x, 5));

Console.WriteLine(result); // Some(10)

Match - Pattern Matching

var message = Option.Some(42).Match(
    some: value => $"Found: {value}",
    none: () => "Not found");

Console.WriteLine(message); // "Found: 42"

ToOption - Convert from Nullable

string? nullableString = "Hello";
var option = nullableString.ToOption();

int? nullableInt = null;
var noneOption = nullableInt.ToOption();

Real-World Example

var users = new Dictionary<int, string>
{
    { 1, "Alice" },
    { 2, "Bob" }
};

Option<string> GetUser(int id)
{
    return users.TryGetValue(id, out var name) 
        ? Option.Some(name) 
        : Option.None<string>();
}

var userName = GetUser(1)
    .Map(name => name.ToUpper())
    .ValueOr("UNKNOWN");

Console.WriteLine(userName); // "ALICE"

API Reference

Option<T> struct

Properties
  • bool HasValue - True if the option contains a value
  • bool IsNone - True if the option is empty
  • T Value - The optional value (throws if empty)
Methods
  • static Option<T> Some(T value) - Create an option with a value
  • static Option<T> None() - Create an empty option
  • T ValueOr(T defaultValue) - Get value or default
  • TResult Match<TResult>(Func<T, TResult> some, Func<TResult> none) - Pattern matching
  • Option<TResult> Map<TResult>(Func<T, TResult> mapper) - Value transformation
  • Option<TResult> Bind<TResult>(Func<T, Option<TResult>> binder) - Monadic bind

Option Static Class

  • Option<T> Some<T>(T value) - Create an option with a value
  • Option<T> None<T>() - Create an empty option

OptionExtensions

  • Option<T> ToOption<T>(this T? value) - Convert nullable reference type
  • Option<T> ToOption<T>(this T? value) where T : struct - Convert nullable value type

License

MIT License

  • Fox.ResultKit - Result<T, TError> pattern implementation
  • Fox.ChainKit - Chain of Responsibility framework

Contributing

Contributions are welcome! Please open an issue or pull request in the GitHub repository.

Support

If you like Fox.OptionKit, please give it a ⭐ on the GitHub repository!

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 is compatible.  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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.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.0.0 96 2/20/2026

Initial release with core Option<T> functionality: Some, None, Map, Bind, Match, ValueOr, and ToOption extension methods.