Infrastructure.Option 10.0.2

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

Build Status

Usage

Get the NuGet package: Infrastructure.Option on NuGet

Overview

The purpose of the Infrastructure.Option is to help you write code that is easier to read and understand.

The Infrastructure.Option makes it explicit when a value might be missing, but it stays out of your sight when irrelevant.

  • Option<T> presents the situation when you don't know if the value is present or not.
  • Option.Some<T> tells that the value of type T is present.
  • Option.None<T> tells that the value of type T is not present.

Option.Some<T> behaves like the object of type T, and you can, e.g., pass it directly to a method accepting a parameter of type T.

Option.None<T> means that the value is not present so you cannot even accidentally try to access it.

The Option provides fluent access to the underlying optional value with Choose() and Otherwise().

  • Choose() applied to a single object selects the chosen property or returns null.
  • Choose() applied to a collection selects the underlying values, i.e., those of type Some<T>.
  • Otherwise() defines fallback behavior when encountering None<T>.

For more information about option types, see Option type on Wikipedia.

JSON Serialization

Infrastructure.Option supports JSON serialization using System.Text.Json without requiring any additional dependencies.

The Option<T> type is serialized as an object with a single ValueOrNull property.

The OpenAPI documentation support is also provided without any additional dependencies.

For example, Option.Some("Hello!") is serialized as:

{ "valueOrNull": "Hello!" }

ToString()

ToString() called on Option.Some returns the result of the underlying object's ToString().

ToString() called on Option.None returns an empty string.

Examples

Basic usage

using Infrastructure;
using System;

void Print(string value) => Console.WriteLine(value);

var something = Option.Some("Something");
var nothing = Option<string>.None;

Print(something); // something is implicitly cast to string.
// Print(nothing); // This does not compile

Creating Options

var some = Option.Some("Example value");
var none = Option.None<string>();

Pattern matching

using Infrastructure;
using System;

var option = Option.Some("Example value");

var value = option switch
{
    { Value: {} some } => some,
    _ => "Something else"
};

Console.WriteLine(value); // Prints: Example value

Choose() underlying value

using Infrastructure;
using System;

Option<Country> optionalCountry = new Country("Finland");

var nameOfTheCountry = optionalCountry.Choose(country => country.Name); // nameOfTheCountry is of type Option<string>

Console.WriteLine(nameOfTheCountry); // Prints: Finland

record Country(string Name);

Fallback with Otherwise()

var option = Option.None<string>();

var result = option.Otherwise("Something else"); // result == "Something else"

Choose() underlying values from collections

var collection = new Option<string>[] {
    Option.None<string>(),
    Option.Some("1"),
    Option.None<string>(),
    Option.Some("2"),
    Option.None<string>(),
    Option.Some("3"),
    Option.None<string>(),
};

var values = collection.Choose(); // values == [ "1", "2", "3" ]
var first = collection.ChooseFirst(); // first == "1"

Choose() underlying values from collections with mapping

record ExampleType(string ExampleProperty);

var collection = new[] {
    Option.None<ExampleType>(),
    Option.Some(new ExampleType("1")),
    Option.None<ExampleType>(),
    Option.Some(new ExampleType("2")),
    Option.None<ExampleType>(),
    Option.Some(new ExampleType("3")),
    Option.None<ExampleType>(),
};

var values = collection.Choose(entry => entry.ExampleProperty); // values == [ "1", "2", "3" ]
var chosen = await collection.Choose(async entry => await Task.FromResult(entry.ExampleProperty)); // chosen == [ "1", "2", "3" ]

Pick singular value from collections

var collection = new Option<string>[] {
    Option.Some("1"),
    Option.Some("2"),
    Option.Some("3")
};

var first = collection.FirstOrNone(); // first == "1"
var firstMatch = collection.FirstOrNone(element => element == "2"); // firstMatch == "2"

var only = collection.SingleOrNone(); // first == "1"
var onlyMatch = collection.SingleOrNone(element => element == "2"); // firstMatch == "2"

Checking if value matches the given predicate

var option = Option.Some("Example value");

var holds = option.Holds(example => example == "Example value"); // holds == true
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 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.

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
10.0.2 336 12/5/2025
10.0.0 173 12/5/2025
8.12.0 2,783 5/22/2025
8.11.0 3,009 4/25/2025
8.10.0 184 4/25/2025
8.9.0 222 4/24/2025
8.8.0 341 4/24/2025
8.6.0 186 4/23/2025
8.5.0 326 4/22/2025
8.4.0 231 4/22/2025
8.3.0 316 4/18/2025
8.2.0 231 4/18/2025
8.1.0 385 4/12/2025
8.0.0 309 4/8/2025
6.0.3 11,798 11/14/2023
6.0.0 356 11/8/2023
1.6.1 34,346 6/4/2020
1.5.0 720 5/31/2020
1.4.0 682 3/4/2020
1.3.0 680 3/2/2020
1.2.0 652 3/2/2020
1.1.0 656 3/1/2020
1.0.0 638 3/1/2020