Table of Contents

Struct Optional<T>

Namespace
Avalonia.Data
Assembly
Avalonia.Base.dll

An optional typed value.

public readonly struct Optional<T> : IEquatable<Optional<T>>

Type Parameters

T

The value type.

Implements
Inherited Members

Remarks

This struct is similar to Nullable<T> except it also accepts reference types: note that null is a valid value for reference types. It is also similar to BindingValue<T> but has only two states: "value present" and "value missing".

To create a new optional value you can:

  • For a simple value, call the Optional<T> constructor or use an implicit conversion from T
  • For an missing value, use Empty or simply default

Constructors

Optional(T)

Initializes a new instance of the Optional<T> struct with value.

public Optional(T value)

Parameters

value T

The value.

Properties

Empty

Returns an Optional<T> without a value.

public static Optional<T> Empty { get; }

Property Value

Optional<T>

HasValue

Gets a value indicating whether a value is present.

public bool HasValue { get; }

Property Value

bool

Value

Gets the value.

public T Value { get; }

Property Value

T

Exceptions

InvalidOperationException

HasValue is false.

Methods

Equals(Optional<T>)

public bool Equals(Optional<T> other)

Parameters

other Optional<T>

Returns

bool

Equals(object?)

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

GetHashCode()

public override int GetHashCode()

Returns

int

GetValueOrDefault()

Gets the value if present, otherwise the default value.

public T? GetValueOrDefault()

Returns

T

The value.

GetValueOrDefault(T)

Gets the value if present, otherwise a default value.

public T? GetValueOrDefault(T defaultValue)

Parameters

defaultValue T

The default value.

Returns

T

The value.

GetValueOrDefault<TResult>()

Gets the value if present, otherwise the default value.

public TResult? GetValueOrDefault<TResult>()

Returns

TResult

The value if present and of the correct type, default(TResult) if the value is not present or of an incorrect type.

Type Parameters

TResult

GetValueOrDefault<TResult>(TResult)

Gets the value if present, otherwise a default value.

public TResult? GetValueOrDefault<TResult>(TResult defaultValue)

Parameters

defaultValue TResult

The default value.

Returns

TResult

The value if present and of the correct type, default(TResult) if the value is present but not of the correct type or null, or defaultValue if the value is not present.

Type Parameters

TResult

ToObject()

Casts the value (if any) to an object.

public Optional<object?> ToObject()

Returns

Optional<object>

The cast optional value.

ToString()

public override string ToString()

Returns

string

Operators

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

Compares two Optional<T>s for equality.

public static bool operator ==(Optional<T> x, Optional<T> y)

Parameters

x Optional<T>

The first value.

y Optional<T>

The second value.

Returns

bool

True if the values are equal; otherwise false.

implicit operator Optional<T>(T)

Creates an Optional<T> from an instance of the underlying value type.

public static implicit operator Optional<T>(T value)

Parameters

value T

The value.

Returns

Optional<T>

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

Compares two Optional<T>s for inequality.

public static bool operator !=(Optional<T> x, Optional<T> y)

Parameters

x Optional<T>

The first value.

y Optional<T>

The second value.

Returns

bool

True if the values are unequal; otherwise false.