Infrastructure.Option
8.8.0
See the version list below for details.
dotnet add package Infrastructure.Option --version 8.8.0
NuGet\Install-Package Infrastructure.Option -Version 8.8.0
<PackageReference Include="Infrastructure.Option" Version="8.8.0" />
<PackageVersion Include="Infrastructure.Option" Version="8.8.0" />
<PackageReference Include="Infrastructure.Option" />
paket add Infrastructure.Option --version 8.8.0
#r "nuget: Infrastructure.Option, 8.8.0"
#addin nuget:?package=Infrastructure.Option&version=8.8.0
#tool nuget:?package=Infrastructure.Option&version=8.8.0
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 typeSome<T>
.Otherwise()
defines fallback behavior when encounteringNone<T>
.
Examples
Creating Some
values
var someString = Option.Some("Example value");
var someInteger = Option.Some(12345);
Creating None
values
var noneString = Option.None<string>();
var noneInteger = Option.None<int>();
Pattern matching optional values
var option = Option.Some("Example value");
var value = option is Some<string> some ? some.Value : "Something else";
Fallback values using fluent syntax
var option = Option.None<string>();
var optionalFallbackValue = Option.Some("Example value");
var fallbackValue = Option.Some("Example value");
var fallbackToOptionalValue = option.Otherwise(optionalFallbackValue);
var fallbackToValue = option.Otherwise(fallbackValue);
var fallbackToOptionalValueProvidedByFactory = option.Otherwise(() => optionalFallbackValue);
var fallbackToValueProvidedByFactory = option.Otherwise(() => fallbackValue);
Choosing the first value from multiple options
var options = new[] {
Option.None<string>(),
Option.Some("Example value")
};
var option = options.ChooseFirst();
Selecting existing values from a collection
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(); // returns [ "1", "2", "3" ]
Selecting values 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 propertyValues = collection.Choose(entry => entry.ExampleProperty); // returns [ "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 | Versions 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. |
-
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,607 | 4/25/2025 |
8.10.0 | 113 | 4/25/2025 |
8.9.0 | 159 | 4/24/2025 |
8.8.0 | 272 | 4/24/2025 |
8.6.0 | 148 | 4/23/2025 |
8.5.0 | 241 | 4/22/2025 |
8.4.0 | 150 | 4/22/2025 |
8.3.0 | 190 | 4/18/2025 |
8.2.0 | 154 | 4/18/2025 |
8.1.0 | 216 | 4/12/2025 |
8.0.0 | 238 | 4/8/2025 |
6.0.3 | 11,145 | 11/14/2023 |
6.0.0 | 278 | 11/8/2023 |
1.6.1 | 33,600 | 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 | 516 | 3/1/2020 |
1.0.0 | 523 | 3/1/2020 |