UnionType 1.5.0

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

// Install UnionType as a Cake Tool
#tool nuget:?package=UnionType&version=1.5.0

<center><h1 align="center">UnionType</h1></center> <div align="center">

Powerful, low consumption, and highly scalable type for c#.

.NET Build codecov

</div>

What is this

It provide an union type struct, for strongly typed c# language.

How to implement

Use Memory mask to make struct has 16(maximum primary type bytes)+1(type store) bytes, it can be store primary types.

Object store use GCHandle to pin memory.

Samples

samples/UnionType.Sample

implicit cast in any time

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        UnionValue a = 123;
        int b = a;//123
    }
}

You can use navite point or ref

using System;
using UnionType;

internal class Program
{
    unsafe static void Main()
    {
        UnionValue a = 123;
        Console.WriteLine(*a.ToPointer<int>());//Print 123
    }
}

It is not a read-only type

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        var uv = (UnionValue)123;
        Inc(ref uv);
        Console.WriteLine(uv.@int);//Print 124
    }
    private static void Inc(ref UnionValue value)
    {
        value.@int++;
    }
}

It can accommodate all basic types

  • void(Empty)
  • byte
  • sbyte
  • bool
  • char
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • float(Single)
  • double
  • decimal
  • object
  • DBNull
  • DateTime
  • String
  • TimeSpan(Additional types)
  • Guid(Additional types)
  • IntPtr(Additional types)

It can fast alloc decimal from number

Performace is alloc decimal benchmark

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        var uv = UnionValue.FromAsDecimal(123);
        Console.WriteLine(uv.@decimal);//123
    }
}

Implement p type interface(No .NET 7)

  • ICloneable
  • IComparable
  • IConvertible
  • IFormattable

Space saving

using System;
using System.Runtime.CompilerServices;
using UnionType;

internal class Program
{
    static void Main()
    {
        Console.WriteLine(Unsafe.SizeOf<UnionValue>());//In .NET7.0 print 24, other print 17
    }
}

Able to operate

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        UnionValue a = 123;
        UnionValue b = 456;

        var result = a + b;
        Console.WriteLine(result.ToString());//579
    }
}

Supports

  • +
  • -
  • *
  • /
  • %

Integer will become long, floating point will become decimal

Can compare

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        UnionValue a = 123;
        var b = 456;
        Console.WriteLine(a > b);//False
        Console.WriteLine(a < b);//True
    }
}

Supports

  • >
  • >=
  • <
  • <=
  • == It will check type
  • != It will check type

Can store object(weak ref)

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        var obj = new Program();
        var uv = new UnionValue { Object = obj };
        Console.WriteLine(uv.TypeNameString);//Program, rsa2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
        Console.WriteLine(uv.Object);//Program
    }
}

No box, raw type, can box if you need

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        var uv = UnionValue.FromAsDecimal(123);
        Console.WriteLine(uv.Box());//123
    }
}

FromObject or raw type

using System;
using UnionType;

internal class Program
{
    static void Main()
    {
        var uv = UnionValue.FromObject(123);//Fast
        Console.WriteLine(uv.Box());//123
        uv = UnionValue.FromObject((object)123);
        Console.WriteLine(uv.Box());//123
    }
}

I use raw point copy!

https://github.com/Cricle/UnionType/blob/eff150cb9a216506ea8d6b3968101165251e431b/src/UnionType/UnionValueCreator.cs#L108

In serialize case.(Can see UnionValueToBytesHelperTest.cs)

Performace


BenchmarkDotNet=v0.13.2, OS=Windows 10 (10.0.19045.2364)
Intel Core i5-9400F CPU 2.90GHz (Coffee Lake), 1 CPU, 6 logical and 6 physical cores
.NET SDK=7.0.100
  [Host]     : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2
  DefaultJob : .NET 7.0.0 (7.0.22.51805), X64 RyuJIT AVX2


Method Count Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
Decimal 500 136.3 ns 0.37 ns 0.33 ns 1.00 0.00 - NA
Union 500 136.5 ns 0.47 ns 0.42 ns 1.00 0.00 - NA
Box 500 136.5 ns 0.66 ns 0.62 ns 1.00 0.00 - NA
BigInteger 500 912.7 ns 3.84 ns 3.59 ns 6.70 0.03 - NA
Decimal 5000000 1,290,235.7 ns 2,928.91 ns 2,596.40 ns 1.00 0.00 - NA
Union 5000000 1,291,152.6 ns 4,300.97 ns 3,812.70 ns 1.00 0.00 1 B NA
Box 5000000 1,291,088.1 ns 3,581.48 ns 2,990.70 ns 1.00 0.00 1 B NA
BigInteger 5000000 9,026,300.8 ns 10,061.51 ns 8,919.27 ns 7.00 0.01 8 B NA
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 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.

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.5.0 82 3/10/2024
1.4.0 211 3/14/2023
1.3.5 260 1/9/2023
1.3.4 294 12/28/2022
1.3.3 248 12/27/2022
1.3.2 249 12/23/2022
1.3.1 407 12/13/2022
1.3.0 286 12/4/2022
1.3.0-preview.2 89 12/2/2022
1.3.0-preview.1 89 10/26/2022
1.2.0 331 10/25/2022
1.1.1 353 10/23/2022
1.1.0 360 10/22/2022
1.0.1 407 10/20/2022
1.0.0 411 10/20/2022