Penca53.Variant 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Penca53.Variant --version 1.0.2
NuGet\Install-Package Penca53.Variant -Version 1.0.2
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="Penca53.Variant" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Penca53.Variant --version 1.0.2
#r "nuget: Penca53.Variant, 1.0.2"
#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 Penca53.Variant as a Cake Addin
#addin nuget:?package=Penca53.Variant&version=1.0.2

// Install Penca53.Variant as a Cake Tool
#tool nuget:?package=Penca53.Variant&version=1.0.2

Variant CSharp

C# implementation of C++ std::variant. An instance of Variant at any given time either holds a value of one of its alternative types, or null. It provides compile-time type safety and highly optimized operations.

Usage

Initialization

null (NONE)

Variant<double, string> explicitConstructorWithNullValue = new Variant<double, string>();

double (T1)

Variant<double, string> explicitConstructor = new Variant<double, string>(1d);
Variant<double, string> implicitContructor = 1d;

string (T2)

Variant<double, string> explicitConstructor = new Variant<double, string>("Hello");
Variant<double, string> implicitContructor = "Hello";

Get

T1

Variant<double, string> variant = 1d;
double value = variant.GetT1();
double value = (double)variant;

T2

Variant<double, string> variant = "Hello";
string value = variant.GetT2();
string value = (string)variant;

Or use TryGet when type isn't known beforehand

Variant<double, string> variant = 1d;
// T1 = double
if (variant.TryGetT1(out double doubleValue)) // True
{

}
// T2 = string
else if (variant.TryGetT2(out string stringValue)  // False
{

}
// NONE = null
else  // False
{

}

Type Check

Type

Variant<double, string> variant = new Variant<double, string>();
int type = variant.Type; // NONE = 0
Variant<double, string> variant = 1d;
int type = variant.Type; // T1 = 1
Variant<double, string> variant = "Hello";
int type = variant.Type; // T2 = 2

Is<T>

Variant<double, string> variant = 1d;
bool isDouble = variant.Is<double>(); // True
bool isString = variant.Is<string>(); // False

Dealing with collections

It may be useful using the Type property as index for a collection.

T1

int[] array = new int[10];
Variant<double, string> variant = 1d;
int index = variant.GetIndex(); // 0
array[index] = 2;

T2

int[] array = new int[10];
Variant<double, string> variant = "Hello";
int index = variant.GetIndex(); // 1
array[index] = 2;

NONE (null value)

int[] array = new int[10];
Variant<double, string> variant = new Variant<double, string>(); // null
int index = variant.GetIndex(); // -1
if (index == -1)
{
  // Variant is empty!
}

Comparison (by value)

Variant<double, string> variantA = 10d;
Variant<double, string> variantB = 10d;
Variant<double, string> variantC = 17d;

bool areEqual = variantA.Equals(variantB); // True
bool areEqual = variantA.Equals(variantC); // False
Variant<double, string> variantA = "Hello";
Variant<double, string> variantB = "Hello";
Variant<double, string> variantC = "World";

bool areEqual = variantA.Equals(variantB); // True
bool areEqual = variantA.Equals(variantC); // False
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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.0.4 348 8/19/2021
1.0.3 283 8/19/2021
1.0.2 313 8/4/2021
1.0.1 253 8/2/2021
1.0.0 257 8/2/2021