Philiprehberger.OptionType 0.1.0

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

Philiprehberger.OptionType

CI NuGet License

Option/Maybe monad for explicit null handling with Map, Bind, Match, and LINQ support.

Installation

dotnet add package Philiprehberger.OptionType

Usage

Creating Options

using Philiprehberger.OptionType;

var some = Option.Some(42);
var none = Option.None<int>();
var fromNullable = Option.From<string>(null); // None
var fromValue = Option.From("hello");         // Some("hello")

Transforming Values

var doubled = Option.Some(21).Map(x => x * 2);  // Some(42)

var parsed = Option.Some("123")
    .Bind(s => int.TryParse(s, out var n) ? Option.Some(n) : Option.None<int>());

var positive = Option.Some(42).Filter(x => x > 0);  // Some(42)
var negative = Option.Some(-1).Filter(x => x > 0);  // None

Pattern Matching

string message = Option.Some(42).Match(
    some: v => $"Got {v}",
    none: () => "Nothing"
);

int value = none.ValueOr(0);           // 0
int must = some.ValueOrThrow();        // 42

LINQ Query Syntax

var result = from x in Option.Some(10)
             from y in Option.Some(20)
             select x + y;  // Some(30)

var filtered = from x in Option.Some(42)
               where x > 0
               select x;  // Some(42)

Nullable Conversion

int? nullable = 42;
var option = nullable.ToOption();  // Some(42)

int? nullValue = null;
var noneOption = nullValue.ToOption();  // None

API

Option (static)

Method Description
Some<T>(T value) Create an Option containing a value
None<T>() Create an empty Option
From<T>(T? value) Create an Option from a nullable value

Option<T> (struct)

Member Description
IsSome True if the option contains a value
IsNone True if the option is empty
Map<U>(Func<T, U>) Transform the contained value
Bind<U>(Func<T, Option<U>>) Chain option-returning functions
Filter(Func<T, bool>) Keep value only if predicate holds
Match<U>(Func<T, U>, Func<U>) Pattern match on the option
ValueOr(T) Get value or default
ValueOrThrow() Get value or throw InvalidOperationException
ToString() String representation
Equals(Option<T>) Value equality
GetHashCode() Hash code

Extension Methods

Method Description
Select LINQ select support (maps value)
SelectMany LINQ multi-from support (binds values)
Where LINQ where support (filters value)
ToOption() Convert nullable to Option
Flatten() Unwrap nested Option<Option<T>>

Development

dotnet build src/Philiprehberger.OptionType.csproj --configuration Release

License

MIT

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
0.2.1 134 4/1/2026
0.2.0 170 3/28/2026
0.1.1 110 3/25/2026
0.1.0 106 3/23/2026