Skip to content

OnixLabs.Core.None

Matthew Layton edited this page Nov 15, 2026 · 2 revisions

None<T> Class

Represents an absent optional value — the case of Optional<T> that holds no value.

Namespace OnixLabs.Core
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Optional<T> → None<T>
Implements IValueEquatable<Optional<T>> (inherited from Optional<T>)

Definition

public sealed class None<T> : Optional<T>
    where T : notnull

Type Parameters

Parameter Description
T The type of the underlying optional value. Constrained to notnull.

Remarks

None<T> is one of the two cases of Optional<T> and signifies that no value is present. Because Optional<T> is a closed class, a C# 15 switch over Some<T> and None<T> is exhaustive without a discard arm. It carries no data and exposes no public members of its own beyond those inherited from Optional<T>.

The constructor is private. A single shared instance is allocated per closed type T and is accessible through the static Optional<T>.None field. You should never need to reference None<T> by type directly — use the static field, the Of factory, or an implicit null conversion to obtain an absent optional.

None<T> inherits all members of Optional<T>. HasValue always returns false for a None<T>. GetValueOrThrow() throws InvalidOperationException. GetValueOrDefault() returns default(T). ToString() returns "None".

Use Optional<T>.IsNone(Optional<T>), HasValue, or a C# pattern match (is None<T>) to detect the absent case.

Examples

using OnixLabs.Core;

// Access the shared singleton.
Optional<int> none = Optional<int>.None;
Console.WriteLine(none.HasValue);          // False
Console.WriteLine(none.ToString());        // None

// Implicit null conversion also yields None.
Optional<string> implicitNone = null;
Console.WriteLine(implicitNone.HasValue);  // False

// Strip-default factory yields None for default values.
Optional<int> stripped = Optional<int>.Of(0);
Console.WriteLine(stripped.HasValue);      // False

// Pattern match to detect absence.
Optional<string> optional = Optional<string>.None;
if (optional is None<string>)
{
    Console.WriteLine("No value present.");
}

// GetValueOrDefault returns the specified fallback.
string result = Optional<string>.None.GetValueOrDefault("fallback"); // "fallback"

See Also

ONIXLabs .NET Library


Clone this wiki locally