UnionTypes.Toolkit.Generator 2.1.0

dotnet add package UnionTypes.Toolkit.Generator --version 2.1.0
                    
NuGet\Install-Package UnionTypes.Toolkit.Generator -Version 2.1.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="UnionTypes.Toolkit.Generator" Version="2.1.0">
  <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.1.0" />
                    
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.1.0
                    
#r "nuget: UnionTypes.Toolkit.Generator, 2.1.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 UnionTypes.Toolkit.Generator@2.1.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=UnionTypes.Toolkit.Generator&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=UnionTypes.Toolkit.Generator&version=2.1.0
                    
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 struct type with @Union in its leading comments and a partial constructor for each case type for the generator to implement.

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.

// @union
public partial struct MyUnion
{
    public partial MyUnion(int value);
    public partial MyUnion(float value);
    public partial MyUnion(string value);
    public partial MyUnion(IManifest value);
    public partial MyUnion(Coordinate value);
    public partial MyUnion(Address value);
}

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

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.1.0 0 9/16/2026
2.0.4 104 9/6/2026
2.0.3 104 9/6/2026
2.0.2 96 9/5/2026
2.0.1 108 8/30/2026
2.0.0 104 8/30/2026
1.2.0 452 2/3/2025
1.1.1 203 2/1/2025
1.1.0 198 1/31/2025
1.0.0 201 1/30/2025