Show / Hide Table of Contents

Struct Option<T>

Option<T> represents an optional value: every Option<T> is either Some and contains a value, or None, and does not.

Implements
IEquatable<Option<T>>
IComparable<Option<T>>
ISpanFormattable
IFormattable
Inherited Members
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: RustyOptions
Assembly: RustyOptions.dll
Syntax
[Serializable]
[JsonConverter(typeof(OptionJsonConverter))]
public readonly struct Option<T> : IEquatable<Option<T>>, IComparable<Option<T>>, ISpanFormattable, IFormattable where T : notnull
Type Parameters
Name Description
T

The type the option might contain.

Constructors

| Edit this page View Source

Option(T)

Creates an Option<T> containing the given value.

NOTE: Nulls are not allowed; a null value will result in a None option.

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

The value to wrap in an Option<T>.

Properties

| Edit this page View Source

IsNone

Returns true if the option is None.

Declaration
public bool IsNone { get; }
Property Value
Type Description
bool
| Edit this page View Source

None

Returns the None option for the specified T.

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

Methods

| Edit this page View Source

AsEnumerable()

Returns an IEnumerable<T> containing either zero or one value, depending on whether the option is None or Some.

Declaration
public IEnumerable<T> AsEnumerable()
Returns
Type Description
IEnumerable<T>

An enumerable containing the option's value, or an empty enumerable.

| Edit this page View Source

AsSpan()

Converts the option into a ReadOnlySpan<T> that contains either zero or one items depending on whether the option is None or Some.

Declaration
public ReadOnlySpan<T> AsSpan()
Returns
Type Description
ReadOnlySpan<T>

A span containing the option's value, or an empty span.

| Edit this page View Source

CompareTo(Option<T>)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

Some compares as less than any None, while two Some compare as their contained values would in T.

Declaration
public int CompareTo(Option<T> other)
Parameters
Type Name Description
Option<T> other
Returns
Type Description
int

-1 if this instance precendes other, 0 if they are equal, and 1 if this instance follows other.

| Edit this page View Source

Equals(Option<T>)

Indicates whether the current object is equal to another object of the same type.

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

An object to compare with this object.

Returns
Type Description
bool

true if the current object is equal to the other parameter; otherwise, false.

| Edit this page View Source

Equals(object?)

Indicates whether the current object is equal to another object.

Declaration
public override bool Equals(object? obj)
Parameters
Type Name Description
object obj

An object to compare with this object.

Returns
Type Description
bool

true if the current object is equal to the obj parameter; otherwise, false.

Overrides
ValueType.Equals(object)
| Edit this page View Source

GetEnumerator()

Returns an enumerator for the option.

Declaration
public IEnumerator<T> GetEnumerator()
Returns
Type Description
IEnumerator<T>

The enumerator.

| Edit this page View Source

GetHashCode()

Retrieves the hash code of the object contained by the Option<T>, if any.

Declaration
public override int GetHashCode()
Returns
Type Description
int

The hash code of the object returned by the IsSome(out T) method, if that method returns true, or zero if it returns false.

Overrides
ValueType.GetHashCode()
| Edit this page View Source

IsSome(out T)

Returns true if the option is Some, and returns the contained value through value.

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

The value contained in the option.

Returns
Type Description
bool

true if the option is Some.

| Edit this page View Source

Match(Action<T>, Action)

If the option is Some, passes the contained value to the onSome function. Otherwise calls the onNone function.

Declaration
public void Match(Action<T> onSome, Action onNone)
Parameters
Type Name Description
Action<T> onSome

The function to call with the contained Some value, if any.

Action onNone

The function to call if the option is None.

Exceptions
Type Condition
ArgumentNullException

Thrown if either onSome or onNone is null.

| Edit this page View Source

Match<T2>(Func<T, T2>, Func<T2>)

Returns the result of executing the onSome or onNone functions, depending on the state of the Option<T>.

Declaration
public T2 Match<T2>(Func<T, T2> onSome, Func<T2> onNone)
Parameters
Type Name Description
Func<T, T2> onSome

The function to pass the value to, if the option is Some.

Func<T2> onNone

The function to call if the option is None.

Returns
Type Description
T2

The value returned by the called function.

Type Parameters
Name Description
T2

The return type of the given functions.

Exceptions
Type Condition
ArgumentNullException

Thrown if either onSome or onNone is null.

| Edit this page View Source

ToString()

Returns the text representation of the value of the current Option<T> object.

Declaration
public override string ToString()
Returns
Type Description
string

The text representation of the value of the current Option<T> object.

Overrides
ValueType.ToString()
| Edit this page View Source

ToString(string?, IFormatProvider?)

Formats the value of the current Option<T> using the specified format.

Declaration
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
Type Name Description
string format

The format to use, or a null reference to use the default format defined for the type of the contained value.

IFormatProvider formatProvider

The provider to use to format the value, or a null reference to obtain the format information from the current locale setting of the operating system.

Returns
Type Description
string

The value of the current instance in the specified format.

| Edit this page View Source

TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)

Tries to format the value of the current instance into the provided span of characters.

Declaration
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
Type Name Description
Span<char> destination

The span in which to write this instance's value formatted as a span of characters.

int charsWritten

When this method returns, contains the number of characters that were written in destination.

ReadOnlySpan<char> format

A span containing the characters that represent a standard or custom format string that defines the acceptable format for destination.

IFormatProvider provider

An optional object that supplies culture-specific formatting information for destination.

Returns
Type Description
bool

true if the formatting was successful; otherwise, false.

| Edit this page View Source

Unwrap()

Returns the contained Some value, or throws an InvalidOperationException if the value is None.

Declaration
public T Unwrap()
Returns
Type Description
T

The value contained in the option.

Exceptions
Type Condition
InvalidOperationException

Thrown if the option does not contain a value.

Operators

| Edit this page View Source

operator ==(Option<T>, Option<T>)

Determines whether one Option is equal to another Option.

Declaration
public static bool operator ==(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the two values are equal.

| Edit this page View Source

operator >(Option<T>, Option<T>)

Determines whether one Option is greater than another Option.

Declaration
public static bool operator >(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the left parameter is greater than the right parameter.

| Edit this page View Source

operator >=(Option<T>, Option<T>)

Determines whether one Option is greater than or equal to another Option.

Declaration
public static bool operator >=(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the left parameter is greater than or equal to the right parameter.

| Edit this page View Source

operator !=(Option<T>, Option<T>)

Determines whether one Option is not equal to another Option.

Declaration
public static bool operator !=(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the two values are not equal.

| Edit this page View Source

operator <(Option<T>, Option<T>)

Determines whether one Option is less than another Option.

Declaration
public static bool operator <(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the left parameter is less than the right parameter.

| Edit this page View Source

operator <=(Option<T>, Option<T>)

Determines whether one Option is less than or equal to another Option.

Declaration
public static bool operator <=(Option<T> left, Option<T> right)
Parameters
Type Name Description
Option<T> left

The first Option to compare.

Option<T> right

The second Option to compare.

Returns
Type Description
bool

true if the left parameter is less than or equal to the right parameter.

Implements

IEquatable<T>
IComparable<T>
ISpanFormattable
IFormattable

Extension Methods

OptionAsyncExtensions.AndThenAsync<T1, T2>(Option<T1>, Func<T1, Task<Option<T2>>>)
OptionAsyncExtensions.AndThenAsync<T1, T2>(Option<T1>, Func<T1, ValueTask<Option<T2>>>)
OptionAsyncExtensions.MapAsync<T1, T2>(Option<T1>, Func<T1, Task<T2>>)
OptionAsyncExtensions.MapAsync<T1, T2>(Option<T1>, Func<T1, ValueTask<T2>>)
OptionAsyncExtensions.MapOrElseAsync<T1, T2>(Option<T1>, Func<T1, Task<T2>>, Func<Task<T2>>)
OptionAsyncExtensions.MapOrElseAsync<T1, T2>(Option<T1>, Func<T1, ValueTask<T2>>, Func<ValueTask<T2>>)
OptionAsyncExtensions.OrElseAsync<T>(Option<T>, Func<Task<Option<T>>>)
OptionAsyncExtensions.OrElseAsync<T>(Option<T>, Func<ValueTask<Option<T>>>)
OptionExtensions.AndThen<T1, T2>(Option<T1>, Func<T1, Option<T2>>)
OptionExtensions.And<T1, T2>(Option<T1>, Option<T2>)
OptionExtensions.Expect<T>(Option<T>, string)
OptionExtensions.Filter<T>(Option<T>, Func<T, bool>)
OptionExtensions.MapOrElse<T1, T2>(Option<T1>, Func<T1, T2>, Func<T2>)
OptionExtensions.MapOr<T1, T2>(Option<T1>, Func<T1, T2>, T2)
OptionExtensions.Map<T1, T2>(Option<T1>, Func<T1, T2>)
OptionExtensions.None<T>(T)
OptionExtensions.OrElse<T>(Option<T>, Func<Option<T>>)
OptionExtensions.Or<T>(Option<T>, Option<T>)
OptionExtensions.Some<T>(T)
OptionExtensions.UnwrapOrElse<T>(Option<T>, Func<T>)
OptionExtensions.UnwrapOr<T>(Option<T>, T)
OptionExtensions.Xor<T>(Option<T>, Option<T>)
OptionExtensions.ZipWith<T1, T2, T3>(Option<T1>, Option<T2>, Func<T1, T2, T3>)
OptionExtensions.Zip<T1, T2>(Option<T1>, Option<T2>)
OptionResultExtensions.OkOrElse<T, TErr>(Option<T>, Func<TErr>)
OptionResultExtensions.OkOr<T, TErr>(Option<T>, TErr)
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX