Infrastructure.Option 8.10.0

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

Build Status

Usage

Get the NuGet package here: Infrastructure.Option on NuGet

Overview

Option<T> is a simple option type usable in C#.

  • Option.Some<T> represents an available value.
  • Option.None<T> represents the absence of a value.

The Option static class provides factory methods to create instances of Option<T>.

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

Basic manipulation of Option is done via the Choose() and Otherwise() functions.

  • Choose() selects the underlying values, i.e., those of type Some<T>.
  • Otherwise() defines fallback behavior when encountering None<T>.

Examples

Creating Options

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

Pattern matching

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

var value = option is Some<string> some
  ? some.Value
  : "Something else";

Fallback values using fluent Otherwise syntax

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" ]

Checking if value matches the given predicate

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

var holds = option.Holds(example => example == "Example value"); // holds == true

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 that contains the wrapped value.

This approach produces idiomatic JSON for both .NET and the broader JSON ecosystem, and the optional value is clearly described in OpenAPI documentation.

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

{ "ValueOrNull": "Hello!" }
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
8.12.0 157 5/22/2025
8.11.0 1,492 4/25/2025
8.10.0 113 4/25/2025
8.9.0 159 4/24/2025
8.8.0 270 4/24/2025
8.6.0 147 4/23/2025
8.5.0 240 4/22/2025
8.4.0 149 4/22/2025
8.3.0 189 4/18/2025
8.2.0 154 4/18/2025
8.1.0 215 4/12/2025
8.0.0 237 4/8/2025
6.0.3 11,128 11/14/2023
6.0.0 278 11/8/2023
1.6.1 33,594 6/4/2020
1.5.0 605 5/31/2020
1.4.0 564 3/4/2020
1.3.0 551 3/2/2020
1.2.0 535 3/2/2020
1.1.0 515 3/1/2020
1.0.0 523 3/1/2020