Show / Hide Table of Contents

Struct Optional<T>

A container object which may or may not contain a value.

Implements
IEquatable<Optional<T>>
IEquatable<T>
IStructuralEquatable
ISerializable
Inherited Members
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Namespace: DotNext
Assembly: DotNext.dll
Syntax
[Serializable]
public struct Optional<T> : IEquatable<Optional<T>>, IEquatable<T>, IStructuralEquatable, ISerializable
Type Parameters
Name Description
T

Type of value.

Constructors

| Improve this Doc View Source

Optional(T)

Constructs non-empty container.

Declaration
public Optional(T value)
Parameters
Type Name Description
T value

A value to be placed into container.

Remarks

The property langword_csharp_IsNull of the constructed object may be true if value is null. The property langword_csharp_IsUndefined of the constructed object is always false.

Properties

| Improve this Doc View Source

Empty

Represents optional container without value.

Declaration
[Obsolete("Use None static property instead")]
public static Optional<T> Empty { get; }
Property Value
Type Description
Optional<T>
Remarks

The property IsUndefined of returned object is always true.

| Improve this Doc View Source

HasValue

Indicates whether the value is present.

Declaration
public bool HasValue { get; }
Property Value
Type Description
Boolean
Remarks

If this property is true then IsUndefined and IsNull equal to false.

| Improve this Doc View Source

IsNull

Indicates that the value is null.

Declaration
public bool IsNull { get; }
Property Value
Type Description
Boolean
Remarks

This property returns true only if this instance was constructed using Optional(T) with null argument.

| Improve this Doc View Source

IsUndefined

Indicates that the value is undefined.

Declaration
public bool IsUndefined { get; }
Property Value
Type Description
Boolean
See Also
None
| Improve this Doc View Source

None

Represents optional container without value.

Declaration
public static Optional<T> None { get; }
Property Value
Type Description
Optional<T>
Remarks

The property IsUndefined of returned object is always true.

| Improve this Doc View Source

Value

If a value is present, returns the value, otherwise throw exception.

Declaration
public T Value { get; }
Property Value
Type Description
T
Exceptions
Type Condition
InvalidOperationException

No value is present.

Methods

| Improve this Doc View Source

Box()

Boxes value encapsulated by this object.

Declaration
public Optional<object> Box()
Returns
Type Description
Optional<Object>

The boxed value.

| Improve this Doc View Source

Convert<TResult>(ValueFunc<T, TResult>)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise returns None.

Declaration
public Optional<TResult> Convert<TResult>(in ValueFunc<T, TResult> mapper)
Parameters
Type Name Description
ValueFunc<T, TResult> mapper

A mapping function to be applied to the value, if present.

Returns
Type Description
Optional<TResult>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

| Improve this Doc View Source

Convert<TResult>(ValueFunc<T, Optional<TResult>>)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise returns None.

Declaration
public Optional<TResult> Convert<TResult>(in ValueFunc<T, Optional<TResult>> mapper)
Parameters
Type Name Description
ValueFunc<T, Optional<TResult>> mapper

A mapping function to be applied to the value, if present.

Returns
Type Description
Optional<TResult>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

| Improve this Doc View Source

Convert<TResult>(Converter<T, TResult>)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise returns None.

Declaration
public Optional<TResult> Convert<TResult>(Converter<T, TResult> mapper)
Parameters
Type Name Description
Converter<T, TResult> mapper

A mapping function to be applied to the value, if present.

Returns
Type Description
Optional<TResult>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

| Improve this Doc View Source

Convert<TResult>(Converter<T, Optional<TResult>>)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise returns None.

Declaration
public Optional<TResult> Convert<TResult>(Converter<T, Optional<TResult>> mapper)
Parameters
Type Name Description
Converter<T, Optional<TResult>> mapper

A mapping function to be applied to the value, if present.

Returns
Type Description
Optional<TResult>

An Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise None.

Type Parameters
Name Description
TResult

The type of the result of the mapping function.

| Improve this Doc View Source

Equals(T)

Determines whether this container stored the same value as the specified value.

Declaration
public bool Equals(T other)
Parameters
Type Name Description
T other

Other value to compare.

Returns
Type Description
Boolean

true if Value is equal to other; otherwise, false.

| Improve this Doc View Source

Equals(Optional<T>)

Determines whether this container stores the same value as other.

Declaration
public bool Equals(Optional<T> other)
Parameters
Type Name Description
Optional<T> other

Other container to compare.

Returns
Type Description
Boolean

true if this container stores the same value as other; otherwise, false.

| Improve this Doc View Source

Equals(Object)

Determines whether this container stores the same value as other.

Declaration
public override bool Equals(object other)
Parameters
Type Name Description
Object other

Other container to compare.

Returns
Type Description
Boolean

true if this container stores the same value as other; otherwise, false.

Overrides
ValueType.Equals(Object)
| Improve this Doc View Source

Equals(Object, IEqualityComparer)

Performs equality check between stored value and the specified value using method Equals(Object, Object).

Declaration
public bool Equals(object other, IEqualityComparer comparer)
Parameters
Type Name Description
Object other

Other object to compare with Value.

IEqualityComparer comparer

The comparer implementing custom equality check.

Returns
Type Description
Boolean

true if other is equal to Value using custom check; otherwise, false.

| Improve this Doc View Source

GetHashCode()

Computes hash code of the stored value.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

The hash code of the stored value.

Overrides
ValueType.GetHashCode()
Remarks

This method calls GetHashCode() for the object Value.

| Improve this Doc View Source

GetHashCode(IEqualityComparer)

Computes hash code for the stored value using method GetHashCode(Object).

Declaration
public int GetHashCode(IEqualityComparer comparer)
Parameters
Type Name Description
IEqualityComparer comparer

The comparer implementing hash code function.

Returns
Type Description
Int32

The hash code of Value.

| Improve this Doc View Source

If(ValueFunc<T, Boolean>)

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

Declaration
public Optional<T> If(in ValueFunc<T, bool> condition)
Parameters
Type Name Description
ValueFunc<T, Boolean> condition

A predicate to apply to the value, if present.

Returns
Type Description
Optional<T>

An Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional.

| Improve this Doc View Source

If(Predicate<T>)

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

Declaration
public Optional<T> If(Predicate<T> condition)
Parameters
Type Name Description
Predicate<T> condition

A predicate to apply to the value, if present.

Returns
Type Description
Optional<T>

An Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional.

| Improve this Doc View Source

Or(T)

Returns the value if present; otherwise return default value.

Declaration
public T Or(T defaultValue)
Parameters
Type Name Description
T defaultValue

The value to be returned if there is no value present.

Returns
Type Description
T

The value, if present, otherwise defaultValue.

| Improve this Doc View Source

OrDefault()

If a value is present, returns the value, otherwise return default value.

Declaration
public T OrDefault()
Returns
Type Description
T

The value, if present, otherwise default.

| Improve this Doc View Source

OrInvoke(ValueFunc<T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public T OrInvoke(in ValueFunc<T> defaultFunc)
Parameters
Type Name Description
ValueFunc<T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

| Improve this Doc View Source

OrInvoke(Func<T>)

Returns the value if present; otherwise invoke delegate.

Declaration
public T OrInvoke(Func<T> defaultFunc)
Parameters
Type Name Description
Func<T> defaultFunc

A delegate to be invoked if value is not present.

Returns
Type Description
T

The value, if present, otherwise returned from delegate.

| Improve this Doc View Source

OrThrow<TException>()

If a value is present, returns the value, otherwise throw exception.

Declaration
public T OrThrow<TException>()

    where TException : Exception, new()
Returns
Type Description
T

The value, if present.

Type Parameters
Name Description
TException

Type of exception to throw.

| Improve this Doc View Source

OrThrow<TException>(ValueFunc<TException>)

If a value is present, returns the value, otherwise throw exception.

Declaration
public T OrThrow<TException>(in ValueFunc<TException> exceptionFactory)

    where TException : Exception
Parameters
Type Name Description
ValueFunc<TException> exceptionFactory

Exception factory.

Returns
Type Description
T

The value, if present.

Type Parameters
Name Description
TException

Type of exception to throw.

| Improve this Doc View Source

OrThrow<TException>(Func<TException>)

If a value is present, returns the value, otherwise throw exception.

Declaration
public T OrThrow<TException>(Func<TException> exceptionFactory)

    where TException : Exception
Parameters
Type Name Description
Func<TException> exceptionFactory

Exception factory.

Returns
Type Description
T

The value, if present.

Type Parameters
Name Description
TException

Type of exception to throw.

| Improve this Doc View Source

ToString()

Returns textual representation of this object.

Declaration
public override string ToString()
Returns
Type Description
String

The textual representation of this object.

Overrides
ValueType.ToString()
| Improve this Doc View Source

TryGet(out T)

Attempts to extract value from container if it is present.

Declaration
public bool TryGet(out T value)
Parameters
Type Name Description
T value

Extracted value.

Returns
Type Description
Boolean

true if value is present; otherwise, false.

| Improve this Doc View Source

TryGet(out T, out Boolean)

Attempts to extract value from container if it is present.

Declaration
public bool TryGet(out T value, out bool isNull)
Parameters
Type Name Description
T value

Extracted value.

Boolean isNull

true if underlying value is null; otherwise, false.

Returns
Type Description
Boolean

true if value is present; otherwise, false.

Operators

| Improve this Doc View Source

BitwiseOr(Optional<T>, Optional<T>)

Returns non-empty container.

Declaration
public static Optional<T> operator |(in Optional<T> first, in Optional<T> second)
Parameters
Type Name Description
Optional<T> first

The first container.

Optional<T> second

The second container.

Returns
Type Description
Optional<T>

The first non-empty container.

See Also
Coalesce<T>(Optional<T>, Optional<T>)
| Improve this Doc View Source

Equality(Optional<T>, Optional<T>)

Determines whether two containers store the same value.

Declaration
public static bool operator ==(in Optional<T> first, in Optional<T> second)
Parameters
Type Name Description
Optional<T> first

The first container to compare.

Optional<T> second

The second container to compare.

Returns
Type Description
Boolean

true, if both containers store the same value; otherwise, false.

| Improve this Doc View Source

ExclusiveOr(Optional<T>, Optional<T>)

Determines whether two containers are empty or have values.

Declaration
public static Optional<T> operator ^(in Optional<T> first, in Optional<T> second)
Parameters
Type Name Description
Optional<T> first

The first container.

Optional<T> second

The second container.

Returns
Type Description
Optional<T>

true, if both containers are empty or have values; otherwise, false.

| Improve this Doc View Source

Explicit(Optional<T> to T)

Extracts value stored in the Optional container.

Declaration
public static explicit operator T(in Optional<T> optional)
Parameters
Type Name Description
Optional<T> optional

The container.

Returns
Type Description
T
Exceptions
Type Condition
InvalidOperationException

No value is present.

| Improve this Doc View Source

False(Optional<T>)

Checks whether the container has no value.

Declaration
public static bool operator false (in Optional<T> optional)
Parameters
Type Name Description
Optional<T> optional

The container to check.

Returns
Type Description
Boolean

true if this container has no value; otherwise, false.

| Improve this Doc View Source

Implicit(T to Optional<T>)

Wraps value into Optional container.

Declaration
public static implicit operator Optional<T>(T value)
Parameters
Type Name Description
T value

The value to convert.

Returns
Type Description
Optional<T>
| Improve this Doc View Source

Inequality(Optional<T>, Optional<T>)

Determines whether two containers store the different values.

Declaration
public static bool operator !=(in Optional<T> first, in Optional<T> second)
Parameters
Type Name Description
Optional<T> first

The first container to compare.

Optional<T> second

The second container to compare.

Returns
Type Description
Boolean

true, if both containers store the different values; otherwise, false.

| Improve this Doc View Source

True(Optional<T>)

Checks whether the container has value.

Declaration
public static bool operator true (in Optional<T> optional)
Parameters
Type Name Description
Optional<T> optional

The container to check.

Returns
Type Description
Boolean

true if this container has value; otherwise, false.

Explicit Interface Implementations

| Improve this Doc View Source

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Declaration
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
Type Name Description
SerializationInfo info
StreamingContext context

Implements

System.IEquatable<T>
System.IEquatable<T>
System.Collections.IStructuralEquatable
System.Runtime.Serialization.ISerializable

Extension Methods

Optional.Coalesce<T>(Optional<T>, Optional<T>)
ValueTypeExtensions.IsOneOf<T>(T, IEnumerable<T>)
ValueTypeExtensions.IsOneOf<T>(T, T[])
Sequence.Skip<TEnumerator, T>(ref TEnumerator, Int32)
ExpressionBuilder.Const<T>(T)
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX