Skip to main content
Unio package icon

Unio

v1.2.3Latest Stable
net10.0net11.0net9.0

Unio provides high-performance, struct-based discriminated unions for C#. Zero-allocation, exhaustive matching, Match, Switch and TryGet patterns and full value semantics. Simple, performant and scalable.

Published
Nov 16, 2026
today
Total Downloads
4.7K
4,685

Install

dotnet add package Unio --version 1.2.3
ProductVersions
.NET
net10.0net11.0net9.0

README

Unio

High-performance discriminated unions for C#. Designed with strongly typed generic fields, full value equality semantics and allocation-free pattern matching for maximum performance and type safety.

Usage

Targets .NET 9, .NET 10 and .NET 11. The core is compatible with Native AOT and trimming, including UnioBase<...> and source-generated named unions. It uses no runtime code generation or reflection.

The package enables IsAotCompatible and verifies reference AOT metadata on .NET 10 and newer. See the Native AOT examples and validation for publish commands.

Union storage and typed matching do not allocate. Accessing Value boxes value types; named union classes, capturing delegates and some formatting paths can allocate.

using Unio;

// Implicit conversion
Unio<int, string> result = 42;
Unio<int, string> error = "not found";

// Exhaustive matching
string text = result.Match(
    i => $"Number: {i}",
    s => $"Text: {s}");

// Allocation-free matching - pass state instead of capturing variables
// (no closure object allocated per call)
string prefix = "Result";
string text2 = result.Match(prefix,
    static (p, i) => $"{p}: {i}",
    static (p, s) => $"{p}: {s}");

// Allocation-free side-effect switch
result.Switch((prefix, Console.Out),
    static (s, i) => s.Out.WriteLine($"{s.prefix}: {i}"),
    static (s, str) => s.Out.WriteLine($"{s.prefix}: {str}"));

// Safe TryGet pattern
if (result.TryGetT0(out int number))
    Console.WriteLine(number);

Changes from Previous Version

No structural changes between v1.2.1 and v1.2.3.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 3 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.