Struct Optional<T>
- Namespace
- DynamicData.Kernel
- Assembly
- DynamicData.dll
The equivalent of a nullable type which works on value and reference types.
public readonly struct Optional<T> : IEquatable<Optional<T>> where T : notnull
Type Parameters
T
The underlying value type of the Nullable<T> generic type.
- Implements
-
IEquatable<Optional<T>>
- Extension Methods
Properties
HasValue
Gets a value indicating whether the current Nullable<T> object has a value.
public bool HasValue { get; }
Property Value
- bool
true if the current Nullable<T> object has a value; false if the current Nullable<T> object has no value.
None
Gets the default valueless optional.
public static Optional<T> None { get; }
Property Value
- Optional<T>
Value
Gets the value of the current Nullable<T> value.
public T Value { get; }
Property Value
- T
The value of the current Nullable<T> object if the HasValue property is true. An exception is thrown if the HasValue property is false.
Exceptions
- InvalidOperationException
The HasValue property is false.
Methods
Create(T?)
Creates the specified value.
public static Optional<T> Create(T? value)
Parameters
value
TThe value.
Returns
- Optional<T>
The optional value.
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
FromOptional(in Optional<T>)
Gets the value from the optional value.
public static T? FromOptional(in Optional<T> value)
Parameters
value
Optional<T>The optional value.
Returns
- T
The value.
GetHashCode()
public override int GetHashCode()
Returns
ToOptional(T?)
Gets the optional from a value.
public static Optional<T> ToOptional(T? value)
Parameters
value
TThe value to get the optional for.
Returns
- Optional<T>
The optional.
ToString()
public override string? ToString()
Returns
Operators
operator ==(in Optional<T>, in Optional<T>)
public static bool operator ==(in Optional<T> left, in Optional<T> right)
Parameters
Returns
explicit operator T?(in Optional<T>)
Explicit cast from option to value.
public static explicit operator T?(in Optional<T> value)
Parameters
value
Optional<T>The value.
Returns
- T
The optional value.
implicit operator Optional<T>(T?)
Implicit cast from the vale to the optional.
public static implicit operator Optional<T>(T? value)
Parameters
value
TThe value.
Returns
- Optional<T>
The optional value.
operator !=(in Optional<T>, in Optional<T>)
public static bool operator !=(in Optional<T> left, in Optional<T> right)