Struct Optional<T>
- Namespace
- ReactiveUI.Extensions.Async
- Assembly
- ReactiveUI.Extensions.dll
Represents an optional value that may or may not be present.
public readonly struct Optional<T> : IEquatable<Optional<T>>
Type Parameters
TThe type of the value that may be contained by the optional.
- Implements
-
IEquatable<Optional<T>>
- Extension Methods
Remarks
Use this struct to indicate the presence or absence of a value without resorting to null references. When an instance has a value, the HasValue property is true and the Value property returns the contained value. If no value is present, HasValue is false and accessing Value throws an exception. This pattern is useful for APIs that need to distinguish between an explicit 'no value' state and a default value.
Constructors
Optional()
Initializes a new instance of the Optional<T> struct.
public Optional()
Remarks
After using this constructor, the HasValue property is set to false, indicating that the Optional<T> instance does not contain a value.
Optional(T)
Initializes a new instance of the Optional<T> struct.
public Optional(T value)
Parameters
valueTThe value to be contained in the Optional<T> instance.
Properties
Empty
Gets an empty instance of the Optional<T> type that contains no value.
public static Optional<T> Empty { get; }
Property Value
- Optional<T>
Remarks
Use this property to represent the absence of a value in a type-safe manner. The returned instance has no value set and IsPresent is false.
HasValue
Gets a value indicating whether the current instance has a valid value assigned.
public bool HasValue { get; }
Property Value
Value
Gets the value contained in the optional object.
public T? Value { get; }
Property Value
- T
Remarks
Accessing this property when the optional object does not have a value will throw an exception. Use the HasValue property to determine whether a value is present before accessing this property.
Methods
Equals(Optional<T>)
public bool Equals(Optional<T> other)
Parameters
otherOptional<T>
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
objobject
Returns
GetHashCode()
public override int GetHashCode()
Returns
ToString()
public override string ToString()
Returns
Operators
operator ==(Optional<T>, Optional<T>)
public static bool operator ==(Optional<T> left, Optional<T> right)
Parameters
Returns
operator !=(Optional<T>, Optional<T>)
public static bool operator !=(Optional<T> left, Optional<T> right)