EHonda.Optional.Core 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EHonda.Optional.Core --version 2.0.0
                    
NuGet\Install-Package EHonda.Optional.Core -Version 2.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="EHonda.Optional.Core" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EHonda.Optional.Core" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EHonda.Optional.Core" />
                    
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 EHonda.Optional.Core --version 2.0.0
                    
#r "nuget: EHonda.Optional.Core, 2.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 EHonda.Optional.Core@2.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=EHonda.Optional.Core&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EHonda.Optional.Core&version=2.0.0
                    
Install as a Cake Tool

Optional

A lightweight, type-safe implementation of the Option pattern for .NET.

Overview

Option<T> represents an optional value that can be in one of two states:

  • Some: Contains a value (which can be null if T is nullable).
  • None: Contains no value.

This distinction is particularly useful when you need to differentiate between a value that was never specified (None) and a value that was explicitly set to null (Some(null)).

Quickstart

using EHonda.Optional.Core;

// Create options
Option<string> some = Option.Some("hello");
Option<string> none = Option.None<string>();
Option<string> implicitSome = "hello";
Option<string> defaultNone = default;

// Check state
if (some.HasValue) 
{
    Console.WriteLine(some.Value);
}

// Retrieve values with fallbacks
string v1 = some.Or("fallback"); // returns "hello"
string v2 = none.Or("fallback"); // returns "fallback"

Motivation: The "Explicit Null" Use Case

This library was built to solve a specific problem in test infrastructure: distinguishing between "use the default value" and "use null".

Consider a helper method for creating a service in a test. You want parameters to be optional so tests only specify what's relevant.

Without Option<T>, using a nullable parameter makes it impossible to distinguish "unspecified" from "explicit null":

// Problem: Can't distinguish between default (null) and explicit null
IService CreateService(IDependency? dependency = null) 
    => new(dependency ?? CreateDependency());

// Both calls look the same to the method:
CreateService();      // Intention: Use default dependency
CreateService(null);  // Intention: Use null dependency (e.g. for null guard tests)

With Option<T>, None is distinct from null:

// Solution: Option<T> distinguishes between None and Some(null)
IService CreateService(Option<IDependency> dependency = default) 
    => new(dependency.Or(CreateDependency()));

// Now the behavior is clear:
CreateService();      // dependency is None -> uses CreateDependency()
CreateService(null);  // dependency is Some(null) -> uses null
Product Compatible and additional computed target framework versions.
.NET 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.

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
3.0.0 8,721 11/22/2025
2.1.0 308 11/21/2025
2.0.0 563 11/19/2025
1.0.0 450 11/19/2025
0.1.0 441 11/19/2025