UnionType 1.5.0
dotnet add package UnionType --version 1.5.0
NuGet\Install-Package UnionType -Version 1.5.0
<PackageReference Include="UnionType" Version="1.5.0" />
<PackageVersion Include="UnionType" Version="1.5.0" />
<PackageReference Include="UnionType" />
paket add UnionType --version 1.5.0
#r "nuget: UnionType, 1.5.0"
#:package UnionType@1.5.0
#addin nuget:?package=UnionType&version=1.5.0
#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#.
</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)bytesbyteboolcharshortushortintuintlongulongfloat(Single)doubledecimalobjectDBNullDateTimeStringTimeSpan(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!
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 | Versions 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. 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. |
-
.NETStandard 2.0
- Microsoft.Bcl.HashCode (>= 1.1.1)
- System.Memory (>= 4.5.5)
-
net6.0
- No dependencies.
-
net8.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.5.0 | 218 | 3/10/2024 | |
| 1.4.0 | 347 | 3/14/2023 | |
| 1.3.5 | 419 | 1/9/2023 | |
| 1.3.4 | 435 | 12/28/2022 | |
| 1.3.3 | 372 | 12/27/2022 | |
| 1.3.2 | 397 | 12/23/2022 | |
| 1.3.1 | 558 | 12/13/2022 | |
| 1.3.0 | 426 | 12/4/2022 | |
| 1.3.0-preview.2 | 198 | 12/2/2022 | |
| 1.3.0-preview.1 | 204 | 10/26/2022 | |
| 1.2.0 | 479 | 10/25/2022 | |
| 1.1.1 | 566 | 10/23/2022 | |
| 1.1.0 | 510 | 10/22/2022 | |
| 1.0.1 | 628 | 10/20/2022 | |
| 1.0.0 | 635 | 10/20/2022 |