FluentPatternMatch 1.0.0

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

Installation

You can install the package via NuGet:

dotnet add package FluentPatternMatch

or by using the NuGet Package Manager Console:

Install-Package FluentPatternMatch

or by adding it to your .csproj file:

<PackageReference Include="FluentPatternMatch" Version="1.0.0" />

FluentPatternMatch<T, TResult>

A robust, fluent, and fully-featured C# pattern-matching utility for expressive, readable, and safe value, predicate, and type-based matching.

Key Features

  • Fluent API for building match expressions
  • Synchronous and asynchronous matching (including async/await support)
  • Value, predicate, and type-based case matching
  • First-match or all-matches (configurable)
  • Comprehensive extension methods for common patterns:
    • Null/not-null
    • Ranges (int, double, decimal)
    • String patterns (contains, starts/ends with, regex)
    • Enum and record matching
    • Collections (map with pattern matching)
  • Discriminated union/type matching (runtime type)
  • Diagnostics and logging of match attempts and results
  • Custom error handling (per-case and global)
  • Chaining and one-liner support for concise code

Usage Examples

// Fluent, extension-based matching
var result = input.Switch<string, int>()
    .Case("A", () => 1)
    .CaseStartsWith("B", () => 2)
    .Default(() => -1);

// Concise tuple-based matching
var quick = n.Match(
    (1, () => "One"),
    (2, () => "Two"),
    (_ => true, () => "Other"));

// Type-based (discriminated union style)
var typed = union.MatchType(
    (typeof(MyUnion.CaseA), _ => "A"),
    (typeof(MyUnion.CaseB), _ => "B"));

// Null/not-null handling
var nullable = nullableString.MatchNull(() => "Null", s => $"Value: {s}");

// Asynchronous matching
var asyncResult = await input.Switch<string, bool>()
    .CaseAsync(x => x == "A", async () => true)
    .DefaultAsync(async () => false);

// Pattern matching over collections
var all = new[] { 1, 2, 3 }.SwitchMany(m => m
    .CaseInRange(1, 2, () => "Low")
    .Default(() => "Other"));

// Regex and advanced string patterns
var regexMatch = input.Switch<string, string>()
    .CaseRegex(@"^\d+$", m => $"Digits: {m.Value}")
    .Default(() => "Not digits");

// Enum, record, and set membership
var enumResult = myEnum.Switch<MyEnum, string>()
    .CaseEnum(e => e == MyEnum.Foo, () => "Foo")
    .Default(() => "Other");

var recordResult = myRecord.Switch<MyRecord, string>()
    .CaseRecord(new MyRecord("X", 1), () => "Matched X/1")
    .Default(() => "Other");

var setResult = value.Switch<int, string>()
    .CaseIsOneOf(new[] { 1, 3, 5 }, () => "Odd")
    .Default(() => "Even");

#License This project is licensed under the MIT License. See the LICENSE file for details.

Support & Donations

If you find this project useful, you can support future development:

Your support is greatly appreciated!

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.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
1.0.0 149 5/24/2025