dotUnion 1.0.0

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

dotUnion - Source Generator

A Source Generator to generate Union types inside C#

As of July 2025 - C# doesn't have proper union types, this library solves this using a source generator to create a closed polymorphic structure.

Also included are analyzers to help with syntax rules and common errors.

Installation

The NuGet package is available in nuget.org, and you can use your IDE or run the following command to install it

nuget install dotUnion

Usage

Once installed, you only need to mark your record type with the [Union] Attribute, This will generate the polymorphic relationship and the corresponding methods.

using dotUnion.Attributes;

[Union]
public partial record Result<T,TE>
{
	partial record Ok(T Value);
	partial record Err(TE Error);
}

Once you have your union you can use the Match method variants to map every union variant into a single value.

Result<User, Error> result = GetUser();

string match = result.Match(
	ok => ok.Value.Name,
	err => err.Error.Message
);

Or Switch method variants to execute a different action for every union variant.


result.Switch(
	ok => RegisterUser(ok.Value),
	err => NotifyError(err.Error)
);

Analyzers

This source generator comes with a set of analyzer rules to help you catch errors in the union type.

Rules

Rule ID Category Default Severity Description
UL1001 Union Error Union target must be partial
UL1002 Union Error Union parents must be partial
UL1003 Union Error Union type cannot be sealed
UL1004 Union Error Union target cannot have base type
UL1005 Union Error Union parent cannot have non-private constructor
UL1006 Union Error Union type can only have one non-generated part
UL2001 Union Error Union member must be partial
UL2002 Union Error Union member cannot be generic
UL2003 Union Error Union member cannot have base type
UL2004 Union Info Union type member must be a record and partial
UL2005 Union Error Union type member must be public

Async Extensions

Async extension methods can be enabled on a per-project basis using the AsyncUnionExtensions property in the .csproj file.

<Project>
	<PropertyGroup>  
		<AsyncUnionExtensions>enable</AsyncUnionExtensions>  
	</PropertyGroup>
</Project>

Once enabled, you have to use the Union() extension to access the Switch() and Match() extension methods for the union tasks, this is due to the lack of covariance in C# types.


Task<Result<User, Error>> resultTask = GetUserAsync();

string match = await resultTask.Union().Match(
	ok => ok.Value.Name,
	err => err.Error.Message
);

Implicit operators

The source generator automatically creates implicit operators for every part of the union if all parts of the union have a different primary constructor.

Union parts with more than one parameter in their constructor are given an implicit operator that takes in a tuple with the same signature as the constructor.

[Union]
partial record Notification
{
	partial record Payment(decimal Amount);
	partial record Refund(decimal Amount, string Reason);
}

Notification payment = 50m;
Notification refund = (50m, "Lost product");
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in 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
1.0.0 216 7/15/2025