Struct OneOf<T1, T2, T3, T4, T5>
Represents a discriminated union that can hold a value of T1, T2, T3, T4 or T5.
public readonly struct OneOf<T1, T2, T3, T4, T5> : IEqualityOperators<OneOf<T1, T2, T3, T4, T5>, OneOf<T1, T2, T3, T4, T5>, bool>
Type Parameters
T1The first possible value type.
T2The second possible value type.
T3The third possible value type.
T4The fourth possible value type.
T5The fifth possible value type.
- Implements
- Inherited Members
- Extension Methods
Methods
Equals(object?)
Determines whether the stored value equals the specified object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the stored value.
Returns
GetHashCode()
Returns the hash code of the stored value.
public override int GetHashCode()
Returns
- int
The hash code of the stored value, or
0when no value is stored.
Switch(Action<T1>, Action<T2>, Action<T3>, Action<T4>, Action<T5>)
Executes one of the provided actions depending on the stored value type.
public void Switch(Action<T1> action1, Action<T2> action2, Action<T3> action3, Action<T4> action4, Action<T5> action5)
Parameters
action1Action<T1>The action to invoke when the stored value is of type
T1.action2Action<T2>The action to invoke when the stored value is of type
T2.action3Action<T3>The action to invoke when the stored value is of type
T3.action4Action<T4>The action to invoke when the stored value is of type
T4.action5Action<T5>The action to invoke when the stored value is of type
T5.
Switch<T>(Func<T1, T>, Func<T2, T>, Func<T3, T>, Func<T4, T>, Func<T5, T>)
Projects the stored value using the provided functions.
public T Switch<T>(Func<T1, T> func1, Func<T2, T> func2, Func<T3, T> func3, Func<T4, T> func4, Func<T5, T> func5)
Parameters
func1Func<T1, T>The function to use when the stored value is of type
T1.func2Func<T2, T>The function to use when the stored value is of type
T2.func3Func<T3, T>The function to use when the stored value is of type
T3.func4Func<T4, T>The function to use when the stored value is of type
T4.func5Func<T5, T>The function to use when the stored value is of type
T5.
Returns
- T
The result produced by the matching projection function, or the default value of
Twhen the corresponding function is null.
Type Parameters
TThe 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, T3, T4, T5>, OneOf<T1, T2, T3, T4, T5>)
Determines whether two instances store equal values.
public static bool operator ==(OneOf<T1, T2, T3, T4, T5> left, OneOf<T1, T2, T3, T4, T5> right)
Parameters
leftOneOf<T1, T2, T3, T4, T5>The first instance to compare.
rightOneOf<T1, T2, T3, T4, T5>The second instance to compare.
Returns
implicit operator T1(OneOf<T1, T2, T3, T4, T5>)
Retrieves the stored T1 value.
public static implicit operator T1(OneOf<T1, T2, T3, T4, T5> oo)
Parameters
ooOneOf<T1, T2, T3, T4, T5>The union to convert.
Returns
- T1
The stored value when it is of type
T1.
implicit operator T2(OneOf<T1, T2, T3, T4, T5>)
Retrieves the stored T2 value.
public static implicit operator T2(OneOf<T1, T2, T3, T4, T5> oo)
Parameters
ooOneOf<T1, T2, T3, T4, T5>The union to convert.
Returns
- T2
The stored value when it is of type
T2.
implicit operator T3(OneOf<T1, T2, T3, T4, T5>)
Retrieves the stored T3 value.
public static implicit operator T3(OneOf<T1, T2, T3, T4, T5> oo)
Parameters
ooOneOf<T1, T2, T3, T4, T5>The union to convert.
Returns
- T3
The stored value when it is of type
T3.
implicit operator T4(OneOf<T1, T2, T3, T4, T5>)
Retrieves the stored T4 value.
public static implicit operator T4(OneOf<T1, T2, T3, T4, T5> oo)
Parameters
ooOneOf<T1, T2, T3, T4, T5>The union to convert.
Returns
- T4
The stored value when it is of type
T4.
implicit operator T5(OneOf<T1, T2, T3, T4, T5>)
Retrieves the stored T5 value.
public static implicit operator T5(OneOf<T1, T2, T3, T4, T5> oo)
Parameters
ooOneOf<T1, T2, T3, T4, T5>The union to convert.
Returns
- T5
The stored value when it is of type
T5.
implicit operator OneOf<T1, T2, T3, T4, T5>(T1)
Creates a OneOf<T1, T2, T3, T4, T5> from a T1 value.
public static implicit operator OneOf<T1, T2, T3, T4, T5>(T1 o)
Parameters
oT1The value to wrap.
Returns
- OneOf<T1, T2, T3, T4, T5>
A OneOf<T1, T2, T3, T4, T5> instance storing
o.
implicit operator OneOf<T1, T2, T3, T4, T5>(T2)
Creates a OneOf<T1, T2, T3, T4, T5> from a T2 value.
public static implicit operator OneOf<T1, T2, T3, T4, T5>(T2 o)
Parameters
oT2The value to wrap.
Returns
- OneOf<T1, T2, T3, T4, T5>
A OneOf<T1, T2, T3, T4, T5> instance storing
o.
implicit operator OneOf<T1, T2, T3, T4, T5>(T3)
Creates a OneOf<T1, T2, T3, T4, T5> from a T3 value.
public static implicit operator OneOf<T1, T2, T3, T4, T5>(T3 o)
Parameters
oT3The value to wrap.
Returns
- OneOf<T1, T2, T3, T4, T5>
A OneOf<T1, T2, T3, T4, T5> instance storing
o.
implicit operator OneOf<T1, T2, T3, T4, T5>(T4)
Creates a OneOf<T1, T2, T3, T4, T5> from a T4 value.
public static implicit operator OneOf<T1, T2, T3, T4, T5>(T4 o)
Parameters
oT4The value to wrap.
Returns
- OneOf<T1, T2, T3, T4, T5>
A OneOf<T1, T2, T3, T4, T5> instance storing
o.
implicit operator OneOf<T1, T2, T3, T4, T5>(T5)
Creates a OneOf<T1, T2, T3, T4, T5> from a T5 value.
public static implicit operator OneOf<T1, T2, T3, T4, T5>(T5 o)
Parameters
oT5The value to wrap.
Returns
- OneOf<T1, T2, T3, T4, T5>
A OneOf<T1, T2, T3, T4, T5> instance storing
o.
operator !=(OneOf<T1, T2, T3, T4, T5>, OneOf<T1, T2, T3, T4, T5>)
Determines whether two instances store different values.
public static bool operator !=(OneOf<T1, T2, T3, T4, T5> left, OneOf<T1, T2, T3, T4, T5> right)
Parameters
leftOneOf<T1, T2, T3, T4, T5>The first instance to compare.
rightOneOf<T1, T2, T3, T4, T5>The second instance to compare.