Aigamo.MatchGenerator 0.0.0-preview013

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

MatchGenerator

Bring exhaustive pattern matching to C# enums and unions with zero boilerplate.

MatchGenerator is a Roslyn source generator that creates Match extension methods for your enums and discriminated-union-like types, enabling concise, expressive, and compile-time safe branching.

Features

  • Generate Match extension methods for enums and unions
  • Exhaustive by design (no missing cases)
  • Attribute-driven (opt-in per type)
  • Supports generics (Match<U>)
  • Respects effective accessibility
  • Zero runtime cost (pure source generation)

Getting Started

1. Install the package

dotnet add package Aigamo.MatchGenerator

2. Annotate your type

Enum example
using Aigamo.MatchGenerator;

[GenerateMatch]
public enum Gender
{
	Male = 1,
	Female,
}
Union example
using Aigamo.MatchGenerator;

[GenerateMatch]
abstract record MaritalStatus;

sealed record Single : MaritalStatus;
sealed record Married : MaritalStatus;
sealed record Divorced : MaritalStatus;
sealed record Widowed : MaritalStatus;

3. Use Match

Enum
var message = gender.Match(
	onMale: () => "male",
	onFemale: () => "female"
);
Union
var message = maritalStatus.Match(
	onSingle: x => "single",
	onMarried: x => "married",
	onDivorced: x => "divorced",
	onWidowed: x => "widowed"
);

Why use MatchGenerator?

Without MatchGenerator

Enum
var message = gender switch
{
	Gender.Male => "male",
	Gender.Female => "female",
	_ => throw new UnreachableException(),
};
Union
var message = maritalStatus switch
{
	Single x => "single",
	Married x => "married",
	Divorced x => "divorced",
	Widowed x => "widowed",
	_ => throw new UnreachableException(),
};

With MatchGenerator

var message = gender.Match(
	onMale: () => "male",
	onFemale: () => "female"
);
  • More concise
  • More readable
  • No default case required
  • Compile-time safety

Exhaustiveness Guarantee

All cases must be handled.

If a new enum value or union type is added:

public enum Gender
{
	Male = 1,
	Female,
	Other,
}

or

sealed record Separated : MaritalStatus;

Existing Match calls will fail to compile until updated. This ensures no cases are missed.

Generated Code (Example)

Enum

internal static class GenderMatchExtensions
{
	public static U Match<U>(
		this Gender value,
		Func<U> onFemale,
		Func<U> onMale
	)
	{
		return value switch
		{
			Gender.Female => onFemale(),
			Gender.Male => onMale(),
			_ => throw new UnreachableException(),
		};
	}
}

Union

internal static class MaritalStatusMatchExtensions
{
	public static U Match<U>(
		this MaritalStatus value,
		Func<Divorced, U> onDivorced,
		Func<Married, U> onMarried,
		Func<Single, U> onSingle,
		Func<Widowed, U> onWidowed
	)
	{
		return value switch
		{
			Divorced x => onDivorced(x),
			Married x => onMarried(x),
			Single x => onSingle(x),
			Widowed x => onWidowed(x),
			_ => throw new UnreachableException(),
		};
	}
}

References

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.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.0.0-preview013 34 4/3/2026
0.0.0-preview012 34 4/2/2026
0.0.0-preview011 41 4/1/2026
0.0.0-preview010 25 4/1/2026
0.0.0-preview009 30 4/1/2026
0.0.0-preview008 30 4/1/2026
0.0.0-preview007 37 3/31/2026
0.0.0-preview006 43 3/30/2026
0.0.0-preview005 43 3/29/2026
0.0.0-preview004 38 3/29/2026
0.0.0-preview003 38 3/29/2026
0.0.0-preview002 39 3/29/2026
0.0.0-preview001 38 3/29/2026