UnionTypes 1.0.1

dotnet add package UnionTypes --version 1.0.1                
NuGet\Install-Package UnionTypes -Version 1.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" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add UnionTypes --version 1.0.1                
#r "nuget: UnionTypes, 1.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.
// Install UnionTypes as a Cake Addin
#addin nuget:?package=UnionTypes&version=1.0.1

// Install UnionTypes as a Cake Tool
#tool nuget:?package=UnionTypes&version=1.0.1                

UnionTypes

NuGet GitHub Workflow Status

Introduction

A union type is a type whose value can be any one of a specific set of types. For example, you may define a union type whose value must be either an int or a string.

Once defined, you can use instances of this union in a type safe way. For example, if you want a list that can contain only string and int values, but no other types, you can create a List<T> whose generic type is your new union type, instead of making a List<object> that could allow values of other types to be added. This way, you don't have to check if each value is a string or int when you read them, because it's statically guaranteed at compile time.

Usage

Dependencies

This package is available on NuGet Gallery.

It can run in .NET Standard 2.0 runtimes and later, as well as .NET Framework 4.5.2 and later.

dotnet add package UnionTypes

Writing

using UnionTypes;

Union<string, int> myUnion = "hi";

IList<Union<string, int>> path = ["xConfiguration", "Network", 1, "DNS", "Server", 3, "Address"];
string message = string.Join(' ', path); // "xConfiguration Network 1 DNS Server 3 Address"

Reading

For each of the union's constituent types, you can test if the value of an instance of the union type is of that constituent type with the HasValue1 and other properties, or ValueIndex. You can get the strongly-typed value with the related Value1 and other properties.

if (path[0].HasValue1) {
    string message = $"First item is the string {path[0].Value1}"; // "First item is xConfiguration"
}
if (path[0] is { HasValue1: true, Value1: var firstItem }) {
    string message = $"First item is {firstItem?.Trim()}"; // "First item is xConfiguration"
}
if (path[0].ValueIndex == Union2Index.Value1) {
    string message = $"First item is the string {path[0].Value1}"; // "First item is xConfiguration"
}
string message = path[0] switch {
    { ValueIndex: Union2Index.Value1, Value1: var first } => $"First item is string {first?.Trim()}",
    { ValueIndex: Union2Index.Value2, Value2: var first } => $"First item is int {Math.Abs(first)}",
};

If you don't need the value to be strongly typed, you can get it as an object using the Value property.

Console.WriteLine(path[0].Value);

Union size

This library allows unions of 2 or more types, up to a maximum limit of 16 types.

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. 
.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 net452 is compatible.  net46 was computed.  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.
  • .NETFramework 4.5.2

    • No dependencies.
  • .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.1 140 4/18/2024
1.0.0 87 4/18/2024