Table of Contents

Struct OneOf<T1, T2>

Namespace
Utils.Objects
Assembly
Utils.dll

Represents a discriminated union that can hold a value of either T1 or T2.

public readonly struct OneOf<T1, T2> : IEqualityOperators<OneOf<T1, T2>, OneOf<T1, T2>, bool>

Type Parameters

T1

The first possible value type.

T2

The second possible value type.

Implements
IEqualityOperators<OneOf<T1, T2>, OneOf<T1, T2>, bool>
Inherited Members
Extension Methods

Methods

Equals(object?)

Determines whether the stored value equals the specified object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the stored value.

Returns

bool

true when the stored value equals obj; otherwise, false.

GetHashCode()

Returns the hash code of the stored value.

public override int GetHashCode()

Returns

int

The hash code of the stored value, or 0 when no value is stored.

Switch(Action<T1>, Action<T2>)

Executes one of the provided actions depending on the stored value type.

public void Switch(Action<T1> action1, Action<T2> action2)

Parameters

action1 Action<T1>

The action to invoke when the stored value is of type T1.

action2 Action<T2>

The action to invoke when the stored value is of type T2.

Switch<T>(Func<T1, T>, Func<T2, T>)

Projects the stored value using the provided functions.

public T Switch<T>(Func<T1, T> func1, Func<T2, T> func2)

Parameters

func1 Func<T1, T>

The function to use when the stored value is of type T1.

func2 Func<T2, T>

The function to use when the stored value is of type T2.

Returns

T

The result produced by the matching projection function, or the default value of T when the corresponding function is null.

Type Parameters

T

The return type of the projection.

ToString()

Returns a string representation of the stored value.

public override string ToString()

Returns

string

The result of calling ToString() on the stored value, or Empty when no value is stored.

Operators

operator ==(OneOf<T1, T2>, OneOf<T1, T2>)

Determines whether two instances store equal values.

public static bool operator ==(OneOf<T1, T2> left, OneOf<T1, T2> right)

Parameters

left OneOf<T1, T2>

The first instance to compare.

right OneOf<T1, T2>

The second instance to compare.

Returns

bool

true when both instances store equal values; otherwise, false.

implicit operator T1(OneOf<T1, T2>)

Retrieves the stored T1 value.

public static implicit operator T1(OneOf<T1, T2> oo)

Parameters

oo OneOf<T1, T2>

The union to convert.

Returns

T1

The stored value when it is of type T1.

implicit operator T2(OneOf<T1, T2>)

Retrieves the stored T2 value.

public static implicit operator T2(OneOf<T1, T2> oo)

Parameters

oo OneOf<T1, T2>

The union to convert.

Returns

T2

The stored value when it is of type T2.

implicit operator OneOf<T1, T2>(T1)

Creates a OneOf<T1, T2> from a T1 value.

public static implicit operator OneOf<T1, T2>(T1 o)

Parameters

o T1

The value to wrap.

Returns

OneOf<T1, T2>

A OneOf<T1, T2> instance storing o.

implicit operator OneOf<T1, T2>(T2)

Creates a OneOf<T1, T2> from a T2 value.

public static implicit operator OneOf<T1, T2>(T2 o)

Parameters

o T2

The value to wrap.

Returns

OneOf<T1, T2>

A OneOf<T1, T2> instance storing o.

operator !=(OneOf<T1, T2>, OneOf<T1, T2>)

Determines whether two instances store different values.

public static bool operator !=(OneOf<T1, T2> left, OneOf<T1, T2> right)

Parameters

left OneOf<T1, T2>

The first instance to compare.

right OneOf<T1, T2>

The second instance to compare.

Returns

bool

true when the instances store different values; otherwise, false.