Struct OneOf<T1, T2, T3, T4, T5>

Namespace
REslava.Result.AdvancedPatterns
Assembly
REslava.Result.dll

Represents a value that can be one of five possible types. A type-safe discriminated union for functional programming patterns.

public readonly struct OneOf<T1, T2, T3, T4, T5> : IEquatable<OneOf<T1, T2, T3, T4, T5>>

Type Parameters

T1

The first possible type.

T2

The second possible type.

T3

The third possible type.

T4

The fourth possible type.

T5

The fifth possible type.

Implements
IEquatable<OneOf<T1, T2, T3, T4, T5>>
Inherited Members
Extension Methods

Remarks

OneOf<T1, T2, T3, T4, T5> provides a type-safe way to represent a value that can be one of five types. This is useful for scenarios where you need to handle multiple distinct states without using null references, exceptions, or complex enums.

OneOf<SuccessData, ValidationError, NotFoundError, ConflictError, ServerError> response = await CallApi();
return response.Match(
    case1: data => ProcessData(data),
    case2: err => HandleValidation(err),
    case3: err => HandleNotFound(err),
    case4: err => HandleConflict(err),
    case5: err => HandleServerError(err)
);

Properties

AsT1

Gets the value as T1 if it contains T1, otherwise throws InvalidOperationException.

public T1 AsT1 { get; }

Property Value

T1

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T1.

AsT2

Gets the value as T2 if it contains T2, otherwise throws InvalidOperationException.

public T2 AsT2 { get; }

Property Value

T2

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T2.

AsT3

Gets the value as T3 if it contains T3, otherwise throws InvalidOperationException.

public T3 AsT3 { get; }

Property Value

T3

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T3.

AsT4

Gets the value as T4 if it contains T4, otherwise throws InvalidOperationException.

public T4 AsT4 { get; }

Property Value

T4

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T4.

AsT5

Gets the value as T5 if it contains T5, otherwise throws InvalidOperationException.

public T5 AsT5 { get; }

Property Value

T5

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T5.

IsT1

Gets whether the OneOf contains a value of type T1.

public bool IsT1 { get; }

Property Value

bool

IsT2

Gets whether the OneOf contains a value of type T2.

public bool IsT2 { get; }

Property Value

bool

IsT3

Gets whether the OneOf contains a value of type T3.

public bool IsT3 { get; }

Property Value

bool

IsT4

Gets whether the OneOf contains a value of type T4.

public bool IsT4 { get; }

Property Value

bool

IsT5

Gets whether the OneOf contains a value of type T5.

public bool IsT5 { get; }

Property Value

bool

Methods

BindT2<TNewT2>(Func<T2, OneOf<T1, TNewT2, T3, T4, T5>>)

Binds the T2 value if present, otherwise propagates other types.

public OneOf<T1, TNewT2, T3, T4, T5> BindT2<TNewT2>(Func<T2, OneOf<T1, TNewT2, T3, T4, T5>> binder)

Parameters

binder Func<T2, OneOf<T1, TNewT2, T3, T4, T5>>

Returns

OneOf<T1, TNewT2, T3, T4, T5>

The result of the binder function or the original T1/T3/T4/T5.

Type Parameters

TNewT2

Exceptions

ArgumentNullException

Thrown when binder is null.

BindT3<TNewT3>(Func<T3, OneOf<T1, T2, TNewT3, T4, T5>>)

Binds the T3 value if present, otherwise propagates other types.

public OneOf<T1, T2, TNewT3, T4, T5> BindT3<TNewT3>(Func<T3, OneOf<T1, T2, TNewT3, T4, T5>> binder)

Parameters

binder Func<T3, OneOf<T1, T2, TNewT3, T4, T5>>

Returns

OneOf<T1, T2, TNewT3, T4, T5>

The result of the binder function or the original T1/T2/T4/T5.

Type Parameters

TNewT3

Exceptions

ArgumentNullException

Thrown when binder is null.

BindT4<TNewT4>(Func<T4, OneOf<T1, T2, T3, TNewT4, T5>>)

Binds the T4 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, TNewT4, T5> BindT4<TNewT4>(Func<T4, OneOf<T1, T2, T3, TNewT4, T5>> binder)

Parameters

binder Func<T4, OneOf<T1, T2, T3, TNewT4, T5>>

Returns

OneOf<T1, T2, T3, TNewT4, T5>

The result of the binder function or the original T1/T2/T3/T5.

Type Parameters

TNewT4

Exceptions

ArgumentNullException

Thrown when binder is null.

Equals(OneOf<T1, T2, T3, T4, T5>)

Indicates whether the current OneOf is equal to another OneOf of the same type.

public bool Equals(OneOf<T1, T2, T3, T4, T5> other)

Parameters

other OneOf<T1, T2, T3, T4, T5>

Returns

bool

Equals(object?)

Indicates whether the current OneOf is equal to another object.

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

FromT1(T1)

Creates a OneOf containing a T1 value.

public static OneOf<T1, T2, T3, T4, T5> FromT1(T1 value)

Parameters

value T1

Returns

OneOf<T1, T2, T3, T4, T5>

FromT2(T2)

Creates a OneOf containing a T2 value.

public static OneOf<T1, T2, T3, T4, T5> FromT2(T2 value)

Parameters

value T2

Returns

OneOf<T1, T2, T3, T4, T5>

FromT3(T3)

Creates a OneOf containing a T3 value.

public static OneOf<T1, T2, T3, T4, T5> FromT3(T3 value)

Parameters

value T3

Returns

OneOf<T1, T2, T3, T4, T5>

FromT4(T4)

Creates a OneOf containing a T4 value.

public static OneOf<T1, T2, T3, T4, T5> FromT4(T4 value)

Parameters

value T4

Returns

OneOf<T1, T2, T3, T4, T5>

FromT5(T5)

Creates a OneOf containing a T5 value.

public static OneOf<T1, T2, T3, T4, T5> FromT5(T5 value)

Parameters

value T5

Returns

OneOf<T1, T2, T3, T4, T5>

GetHashCode()

Returns the hash code for the OneOf.

public override int GetHashCode()

Returns

int

MapT2<TNewT2>(Func<T2, TNewT2>)

Maps the T2 value if present, otherwise propagates other types.

public OneOf<T1, TNewT2, T3, T4, T5> MapT2<TNewT2>(Func<T2, TNewT2> mapper)

Parameters

mapper Func<T2, TNewT2>

Returns

OneOf<T1, TNewT2, T3, T4, T5>

A new OneOf with the mapped T2 value or the original T1/T3/T4/T5.

Type Parameters

TNewT2

Exceptions

ArgumentNullException

Thrown when mapper is null.

MapT3<TNewT3>(Func<T3, TNewT3>)

Maps the T3 value if present, otherwise propagates other types.

public OneOf<T1, T2, TNewT3, T4, T5> MapT3<TNewT3>(Func<T3, TNewT3> mapper)

Parameters

mapper Func<T3, TNewT3>

Returns

OneOf<T1, T2, TNewT3, T4, T5>

A new OneOf with the mapped T3 value or the original T1/T2/T4/T5.

Type Parameters

TNewT3

Exceptions

ArgumentNullException

Thrown when mapper is null.

MapT4<TNewT4>(Func<T4, TNewT4>)

Maps the T4 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, TNewT4, T5> MapT4<TNewT4>(Func<T4, TNewT4> mapper)

Parameters

mapper Func<T4, TNewT4>

Returns

OneOf<T1, T2, T3, TNewT4, T5>

A new OneOf with the mapped T4 value or the original T1/T2/T3/T5.

Type Parameters

TNewT4

Exceptions

ArgumentNullException

Thrown when mapper is null.

Match<TResult>(Func<T1, TResult>, Func<T2, TResult>, Func<T3, TResult>, Func<T4, TResult>, Func<T5, TResult>)

Pattern matching — executes the appropriate function based on the contained type.

public TResult Match<TResult>(Func<T1, TResult> case1, Func<T2, TResult> case2, Func<T3, TResult> case3, Func<T4, TResult> case4, Func<T5, TResult> case5)

Parameters

case1 Func<T1, TResult>
case2 Func<T2, TResult>
case3 Func<T3, TResult>
case4 Func<T4, TResult>
case5 Func<T5, TResult>

Returns

TResult

Type Parameters

TResult

The type of the result.

Exceptions

ArgumentNullException

Thrown when any case function is null.

Switch(Action<T1>, Action<T2>, Action<T3>, Action<T4>, Action<T5>)

Executes an action based on the contained type.

public void Switch(Action<T1> case1, Action<T2> case2, Action<T3> case3, Action<T4> case4, Action<T5> case5)

Parameters

case1 Action<T1>
case2 Action<T2>
case3 Action<T3>
case4 Action<T4>
case5 Action<T5>

Exceptions

ArgumentNullException

Thrown when any case action is null.

ToString()

Returns a string representation of the OneOf for debugging.

public override string ToString()

Returns

string

Operators

operator ==(OneOf<T1, T2, T3, T4, T5>, OneOf<T1, T2, T3, T4, T5>)

Determines whether two OneOf instances are equal.

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

Parameters

left OneOf<T1, T2, T3, T4, T5>
right OneOf<T1, T2, T3, T4, T5>

Returns

bool

implicit operator OneOf<T1, T2, T3, T4, T5>(T1)

Implicit conversion from T1 to OneOf<T1, T2, T3, T4, T5>.

public static implicit operator OneOf<T1, T2, T3, T4, T5>(T1 value)

Parameters

value T1

Returns

OneOf<T1, T2, T3, T4, T5>

implicit operator OneOf<T1, T2, T3, T4, T5>(T2)

Implicit conversion from T2 to OneOf<T1, T2, T3, T4, T5>.

public static implicit operator OneOf<T1, T2, T3, T4, T5>(T2 value)

Parameters

value T2

Returns

OneOf<T1, T2, T3, T4, T5>

implicit operator OneOf<T1, T2, T3, T4, T5>(T3)

Implicit conversion from T3 to OneOf<T1, T2, T3, T4, T5>.

public static implicit operator OneOf<T1, T2, T3, T4, T5>(T3 value)

Parameters

value T3

Returns

OneOf<T1, T2, T3, T4, T5>

implicit operator OneOf<T1, T2, T3, T4, T5>(T4)

Implicit conversion from T4 to OneOf<T1, T2, T3, T4, T5>.

public static implicit operator OneOf<T1, T2, T3, T4, T5>(T4 value)

Parameters

value T4

Returns

OneOf<T1, T2, T3, T4, T5>

implicit operator OneOf<T1, T2, T3, T4, T5>(T5)

Implicit conversion from T5 to OneOf<T1, T2, T3, T4, T5>.

public static implicit operator OneOf<T1, T2, T3, T4, T5>(T5 value)

Parameters

value T5

Returns

OneOf<T1, T2, T3, T4, T5>

operator !=(OneOf<T1, T2, T3, T4, T5>, OneOf<T1, T2, T3, T4, T5>)

Determines whether two OneOf instances are not equal.

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

Parameters

left OneOf<T1, T2, T3, T4, T5>
right OneOf<T1, T2, T3, T4, T5>

Returns

bool