Purview.Unions 1.0.0-prerelease.3

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

Purview.Unions

NuGet version Release

Dependency-free C# 15 unions that do not box value-type cases. The union keyword stores one object?, so every value-type case is boxed. This package generates the language's custom union type instead: a typed field per case, a public constructor per case, and a non-boxing TryGetValue. The compiler still applies union conversions, exhaustive switch and is/null patterns.

Install

dotnet add package Purview.Unions

Quick start

using Purview.Unions;

[GenerateUnion(typeof(int), typeof(string))]
public readonly partial struct IntOrString;

IntOrString value = 42;

Console.WriteLine(value switch
{
    int number => $"int {number}",
    string text => $"string {text}",
    null => "empty",
});

The package carries the merged generator and code fixes under analyzers/dotnet/cs, so no second PackageReference is needed.

Declaration surfaces

Surface Shape
A [GenerateUnion(typeof(int), typeof(string))] public readonly partial struct IntOrString;
B [GenerateUnion] public readonly partial struct Option<T> { internal static partial Option<T> Some(Some<T> value); }
C a [GenerateUnion] union implementing its own nested IUnionMembers provider
D Purview.Unions.Union<int, string> value = 42;

Use B or C for a generic union: typeof(T) is not a legal attribute argument (CS0416).

Boxing

Operation Allocates?
union conversion, TryGetValue, is, switch, is null No
reading Value Yes, for a value-type case

Value is part of the language's union contract and boxes. That is inherent to the contract; pattern matching does not use it.

Requirements

  • .NET 11 SDK (C# 15, GA).
  • net10.0 or net11.0. A net11.0 project gets C# 15 by default and needs no LangVersion; a net10.0 project sets <LangVersion>latest</LangVersion> because its default is C# 14. On net10.0 the generator emits internal [Embedded] polyfills; net11.0 uses the BCL types and never receives a duplicate.
  • Do not set EnablePreviewFeatures; LangVersion=latest (only on net10.0) is enough.

Supported declarations

struct, readonly struct, class, record struct and record class, non-generic and generic, with classes, structs, records, interfaces, type parameters, nullable value types and other unions as cases.

A nullable value-type case is exposed through its underlying type: int? becomes case int, the constructor takes int?, and TryGetValue(out int) returns false for null.

Diagnostics

UNI0001–UNI0016 and UNI9000. See the Diagnostics page. Code fixes add a missing partial (UNI0001), make an inaccessible case type public (UNI0005) and, on the fallback path, the [Union] attribute (UNI0007). UNI0000, UNI9001 and UNI9002 are suppression descriptors, not rules: they suppress CA1034, CA1515 and CA1000 on a union's nested IUnionMembers provider.

Switches

Property Default Purpose
DisableUnionsSourceGenerator false Disables generation and the polyfills

Agent skills

The purview-unions-quickstart, purview-unions-modelling, purview-unions-diagnostics and purview-unions-interop agent skills cover this package. No package ships skills: install them from the git repository with the skills CLI:

npx skills add purview-dev/unions

Licence

MIT.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.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-prerelease.3 48 11/12/2026
1.0.0-prerelease.2 44 11/12/2026
1.0.0-prerelease.1 49 11/11/2026