Unions.Pure.Csharp 1.0.8

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

C# Pure Unions

NuGet Version NuGet Downloads Build

Roslyn source generator for minimal-overhead discriminated unions. Add [UnionMember] attributes to a partial type and the generator emits a compact tag enum, factory helpers, and Match/Switch helpers without extra allocations.

Getting started

  1. Install the package in your project:
dotnet add package Unions.Pure.Csharp
  1. Annotate a partial type with the union members you need:
using Unions.Pure.Csharp;

namespace Sample;

[UnionMember(typeof(string))]
[UnionMember(typeof(int))]
[UnionGenerator(GenerateTarget.Visitor | GenerateTarget.TryOut)]
public partial record struct PaymentStatus;
  1. Use the generated factories and helpers:
var paid = PaymentStatus.FromInt32(200);

var message = paid.Match(
    onString: s => $"Message: {s}",
    onInt32: code => $"Code: {code}");

[UnionMember] is injected by the generator; no manual reference is required beyond the package itself.

API choices (performance vs ergonomics)

Generated unions expose three ways to consume values:

  • GenerateTarget.IndirectLambdas: delegate-based Match / Switch (Func<> / Action<>)

    • Pros: nicest call site; familiar functional style.
    • Cons: still does delegate Invoke (typically an indirect call); capturing lambdas allocate (closures/delegates).
  • GenerateTarget.Visitor: visitor-based Match<TVisitor, TResult> / Switch<TVisitor> (generic struct visitors)

    • Pros: avoids delegate Invoke; JIT can inline; no allocations.
    • Cons: requires a small struct visitor type (can be local/private).
  • GenerateTarget.TryOut: inline IsX() / TryGetX(out ...)

    • Pros: “plain C#” if/else; no delegates; no visitors; no indirect calls.
    • Cons: more boilerplate at call site vs Match.

Note on OneOf (comparison)

OneOf is a solid library, but for hot paths it has the same core drawback as any delegate-based API: Match/Switch ultimately invoke delegates (indirect calls), and capturing lambdas allocate. It also needs to handle invalid state (cold path that throws).

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
2.0.12 5,234 6/18/2026
2.0.11 168 6/18/2026
2.0.10 318 5/15/2026
2.0.9 124 5/15/2026
2.0.8 127 5/15/2026
2.0.7 126 5/15/2026
2.0.6 139 5/15/2026
2.0.5 149 2/18/2026
2.0.4 133 2/18/2026
2.0.3 2,628 1/15/2026
2.0.2 338 12/18/2025
2.0.1 315 12/18/2025
2.0.0 752 12/15/2025
1.1.0 296 12/15/2025
1.0.10 271 12/15/2025
1.0.8 259 12/14/2025
1.0.7 252 12/14/2025
1.0.6 261 12/14/2025
1.0.5 259 12/14/2025
1.0.4 256 12/14/2025