Struct Optional<T>
An optional typed value.
public readonly struct Optional<T> : IEquatable<Optional<T>>
Type Parameters
T
The value type.
- Implements
-
IEquatable<Optional<T>>
- 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
TThe 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
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
Equals(object?)
public override bool Equals(object? obj)
Parameters
obj
object
Returns
GetHashCode()
public override int GetHashCode()
Returns
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
TThe 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
TResultThe 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, ordefaultValue
if the value is not present.
Type Parameters
TResult
ToObject()
Casts the value (if any) to an object.
public Optional<object?> ToObject()
Returns
ToString()
public override string ToString()
Returns
Operators
operator ==(Optional<T>, Optional<T>)
Compares two Optional<T>s for equality.
public static bool operator ==(Optional<T> x, Optional<T> y)
Parameters
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
TThe 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
Returns
- bool
True if the values are unequal; otherwise false.