Table of Contents

Struct Maybe<T>

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Represents an optional value that may or may not exist. Similar to Nullable<T> but works with both value and reference types. Use this type to make optionality explicit in your domain model and avoid null reference exceptions.

public readonly struct Maybe<T> : IEquatable<T>, IEquatable<Maybe<T>>

Type Parameters

T

The type of the optional value. Must be a non-null type.

Implements
Inherited Members
Extension Methods

Examples

// Create a Maybe with a value
Maybe<string> name = "John";
if (name.HasValue) Console.WriteLine(name.Value);

// Create an empty Maybe
Maybe<string> noName = Maybe.None<string>();
string result = noName.GetValueOrDefault("Default");

Properties

HasNoValue

Gets a value indicating whether this instance contains no value.

public bool HasNoValue { get; }

Property Value

bool

True if no value is present; otherwise false.

Remarks

This is the logical inverse of HasValue. Use whichever makes your code more readable.

HasValue

Gets a value indicating whether this instance contains a value.

public bool HasValue { get; }

Property Value

bool

True if a value is present; otherwise false.

Value

Gets the underlying value if present; otherwise throws an exception.

public T Value { get; }

Property Value

T

The underlying value of type T.

Remarks

Always check HasValue before accessing this property, or use GetValueOrDefault(T) instead.

Exceptions

InvalidOperationException

Thrown when HasValue is false.

Methods

Equals(Maybe<T>)

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

public bool Equals(Maybe<T> other)

Parameters

other Maybe<T>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

Equals(T?)

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

public bool Equals(T? other)

Parameters

other T

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

GetValueOrDefault(T)

Gets the underlying value if present, otherwise returns the specified default value.

public T GetValueOrDefault(T defaultValue)

Parameters

defaultValue T

The value to return when HasValue is false.

Returns

T

The underlying value or defaultValue.

Remarks

This is the recommended way to safely extract values from Maybe.

GetValueOrThrow(string?)

Gets the underlying value if present, otherwise throws an exception.

public T GetValueOrThrow(string? errorMessage = null)

Parameters

errorMessage string

Optional custom error message to use when throwing.

Returns

T

The underlying value of type T.

Remarks

Prefer GetValueOrDefault(T) or TryGetValue(out T) to avoid exceptions.

Exceptions

InvalidOperationException

Thrown when HasValue is false.

ToString()

Returns the fully qualified type name of this instance.

public override string ToString()

Returns

string

The fully qualified type name.

TryGetValue(out T)

Attempts to get the underlying value without throwing an exception.

public bool TryGetValue(out T value)

Parameters

value T

When this method returns true, contains the underlying value; otherwise, the default value for type T.

Returns

bool

True if a value is present; otherwise false.

Remarks

Similar to the TryParse pattern in .NET, this provides a safe way to check for and retrieve values.

Operators

operator ==(Maybe<T>, Maybe<T>)

Determines whether two Maybe<T> instances are equal.

public static bool operator ==(Maybe<T> first, Maybe<T> second)

Parameters

first Maybe<T>

The first Maybe instance.

second Maybe<T>

The second Maybe instance.

Returns

bool

True if both have no value, or both have equal values; otherwise false.

operator ==(Maybe<T>, object?)

Determines whether a Maybe<T> equals another object.

public static bool operator ==(Maybe<T> maybe, object? other)

Parameters

maybe Maybe<T>

The Maybe instance to compare.

other object

The object to compare against.

Returns

bool

True if equal; otherwise false.

operator ==(Maybe<T>, T)

Determines whether a Maybe<T> equals a value of type T.

public static bool operator ==(Maybe<T> maybe, T value)

Parameters

maybe Maybe<T>

The Maybe instance to compare.

value T

The value to compare against.

Returns

bool

True if the Maybe has a value equal to value; otherwise false.

implicit operator Maybe<T>(T?)

Implicitly converts a value of type T to a Maybe<T>.

public static implicit operator Maybe<T>(T? value)

Parameters

value T

The value to wrap. If null, creates an empty Maybe.

Returns

Maybe<T>

A Maybe containing the value, or an empty Maybe if value is null.

operator !=(Maybe<T>, Maybe<T>)

Determines whether two Maybe<T> instances are not equal.

public static bool operator !=(Maybe<T> first, Maybe<T> second)

Parameters

first Maybe<T>

The first Maybe instance.

second Maybe<T>

The second Maybe instance.

Returns

bool

True if the instances are not equal; otherwise false.

operator !=(Maybe<T>, object?)

Determines whether a Maybe<T> does not equal another object.

public static bool operator !=(Maybe<T> maybe, object? other)

Parameters

maybe Maybe<T>

The Maybe instance to compare.

other object

The object to compare against.

Returns

bool

True if not equal; otherwise false.

operator !=(Maybe<T>, T)

Determines whether a Maybe<T> does not equal a value of type T.

public static bool operator !=(Maybe<T> maybe, T value)

Parameters

maybe Maybe<T>

The Maybe instance to compare.

value T

The value to compare against.

Returns

bool

True if the Maybe does not have a value equal to value; otherwise false.