UnionTypes.Toolkit.Generator 2.0.1

dotnet add package UnionTypes.Toolkit.Generator --version 2.0.1
                    
NuGet\Install-Package UnionTypes.Toolkit.Generator -Version 2.0.1
                    
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="UnionTypes.Toolkit.Generator" Version="2.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="UnionTypes.Toolkit.Generator" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="UnionTypes.Toolkit.Generator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 UnionTypes.Toolkit.Generator --version 2.0.1
                    
#r "nuget: UnionTypes.Toolkit.Generator, 2.0.1"
                    
#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 UnionTypes.Toolkit.Generator@2.0.1
                    
#: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=UnionTypes.Toolkit.Generator&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=UnionTypes.Toolkit.Generator&version=2.0.1
                    
Install as a Cake Tool

UnionTypes.Toolkit.Generator

A C# source generator library for generating custom union types compatible with the C# union types feature.

It currently implements a single generator that uses techniques to avoid boxing and minimize memory footprint.

The generator is purely standalone; the generated union source does not depend on other libraries at runtime to function.

It may include additional generators in the future.

Declaring a Non-Boxing Custom Union Type

Declare a partial record struct type with a partial void Cases method, whose parameters denote the case types for the union. The names of the parameters are not used, so any name will do.

public partial record struct MyUnion
{
    partial void Cases(
        int case1, 
        float case2,
        string case3,        
        IManifest case4,
        Coordinate case5,
        Address case6
        );
}

record struct Coordinate(float Longitude, float Latitude);
record struct Address(int Id, string Name);
interface IManifest { ... }

The generator will layout the contents of the custom union so that the fields storing the different case types are overlapped with each other using the same memory space, if possible, or at least consume less space than simply having separate fields for each case type. No case value is boxed.

In this example, there will be a single field storing a struct that contains enough space to store either an int, float, Coordinate or the address Id and a sparate object field used to store either a string, IManifest or the address Name.

Learn how to customize the union generation further

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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.1 48 8/30/2026
2.0.0 56 8/30/2026
1.2.0 449 2/3/2025
1.1.1 201 2/1/2025
1.1.0 195 1/31/2025
1.0.0 199 1/30/2025