Skip to main content

Optional<T> Struct

Definition

Namespace:Avalonia.Data
Assembly:Avalonia.Base
Package:Avalonia

An optional typed value.

public struct Optional<T>

Inheritance: ValueType -> Optional<T>

Implements: IEquatable<Optional<>>

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 Avalonia.Data.BindingValue<T> but has only two states: "value present" and "value missing".

To create a new optional value you can:

Constructors

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

Optional<T> Constructor

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

public Optional<T>(T value)

Parameters

value T

The value.

Methods

NameDescription
Equals (2 overloads)No summary available.
GetHashCodeNo summary available.
GetValueOrDefault (3 overloads)Gets the value if present, otherwise the default value.
ToObjectCasts the value (if any) to an object.
ToStringNo summary available.

Equals overloads

Equals Method

public bool Equals(Avalonia.Data.Optional<T><T> other)
Parameters

other Avalonia.Data.Optional<T><T>

Returns

bool

Equals Method

public bool Equals(object obj)
Parameters

obj object

Returns

bool

GetHashCode Method

public int GetHashCode()

Returns

int

GetValueOrDefault overloads

GetValueOrDefault Method

Gets the value if present, otherwise the default value.

public TResult GetValueOrDefault<TResult>()
Returns

T

The value.

GetValueOrDefault Method

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 Method

public TResult GetValueOrDefault<TResult>(TResult defaultValue)
Parameters

defaultValue TResult

Type Parameters

TResult

Returns

TResult

ToObject Method

Casts the value (if any) to an object.

public Avalonia.Data.Optional<T><object> ToObject()

Returns

Avalonia.Data.Optional<T><object>

The cast optional value.

ToString Method

public string ToString()

Returns

string

Properties

NameDescription
EmptyReturns an Avalonia.Data.Optional<T> without a value.
HasValueGets a value indicating whether a value is present.
ValueGets the value.

Empty Property

Returns an Avalonia.Data.Optional<T> without a value.

public Avalonia.Data.Optional<T><T> Empty { get; set; }

HasValue Property

Gets a value indicating whether a value is present.

public bool HasValue { get; set; }

Value Property

Gets the value.

public T Value { get; set; }

Exceptions