Table of Contents

Class MaybeExtensions

Namespace
Existential
Assembly
Existential.Net.dll

Provides extension methods for IEnumerable<T>, and for some specific types of Maybe<T>, to support the use of Maybe<T> with types such as:

  • IEnumerable<T>,
  • IEnumerable<Maybe<T>>,
  • Maybe<string>,
  • Maybe<Guid>,
  • Maybe<T[]>, and
  • Maybe<List<T>>
These methods make it easier to work with Maybe<T> in a variety of contexts.
public static class MaybeExtensions
Inheritance
System.Object
MaybeExtensions
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()

Methods

GetValueOrEmpty(Maybe<Guid>)

Returns the value, if it exists, or System.Guid.Empty.

public static Guid GetValueOrEmpty(this Maybe<Guid> inMaybe)

Parameters

inMaybe Maybe<System.Guid>

The Maybe<T> (where T is System.Guid) that may or may not contain a value.

Returns

System.Guid

The value, if it exists, or System.Guid.Empty.

Remarks

Implemented as an extension method to keep it type-specific to GUIDs.

GetValueOrEmpty(Maybe<String>)

Returns the value, if it exists, or the empty string.

public static string GetValueOrEmpty(this Maybe<string> inMaybe)

Parameters

inMaybe Maybe<System.String>

The Maybe<T> (where T is System.String) that may or may not contain a value.

Returns

System.String

The value, if it exists, or the empty string.

Remarks

Implemented as an extension method to keep it type-specific to strings.

GetValueOrEmpty<T>(Maybe<T[]>)

Returns the value, if it exists, or an empty array of the same type.

public static T[] GetValueOrEmpty<T>(this Maybe<T[]> inMaybe)

Parameters

inMaybe Maybe<T[]>

The Maybe<T> that may or may not contain an array.

Returns

T[]

The value, if it exists, or an empty array of the same type.

Type Parameters

T

The type contained by the array.

Remarks

Implemented as an extension method to keep it type-specific to arrays of T.

GetValueOrEmpty<T>(Maybe<List<T>>)

Returns the value, if it exists, or an empty collection of the same type.

public static List<T> GetValueOrEmpty<T>(this Maybe<List<T>> inMaybe)

Parameters

inMaybe Maybe<System.Collections.Generic.List<T>>

The Maybe<T> that may or may not contain a generic collection.

Returns

System.Collections.Generic.List<T>

The value, if it exists, or an empty collection of the same type.

Type Parameters

T

The type of elements in the collection.

Remarks

Implemented as an extension method to keep it type-specific to generic collections of T.

MaybeFirst<T>(IEnumerable<T>)

Gets a Maybe<T> containing the first member of the collection if it exists.

public static Maybe<T> MaybeFirst<T>(this IEnumerable<T> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

A collection in which to find the first member.

Returns

Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters

T

The type of the collection members.

Exceptions

System.InvalidOperationException

An System.InvalidOperationException could be thrown if the collection is modified after the enumerator was created and before its use.

MaybeFirstThatExists<T>(IEnumerable<T>)

Gets a Maybe<T> containing the first matching member of the collection, if it exists.

public static Maybe<T> MaybeFirstThatExists<T>(this IEnumerable<T> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

A collection in which to find the first existing member.

Returns

Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters

T

The type of the collection members.

Exceptions

System.InvalidOperationException

An System.InvalidOperationException could be thrown if the collection is modified after the enumerator was created and before its use.

MaybeFirstWhere<T>(IEnumerable<T>, Func<T, Boolean>)

Gets a Maybe<T> containing the first matching member of the collection, if it exists.

public static Maybe<T> MaybeFirstWhere<T>(this IEnumerable<T> inCollection, Func<T, bool> inPredicate)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

A collection in which to find the first matching member.

inPredicate System.Func<T, System.Boolean>

The condition that a collection member must satisfy.

Returns

Maybe<T>

A Maybe<T> containing the first member if it exists.

Type Parameters

T

The type of the collection members.

Exceptions

System.ArgumentNullException

An System.ArgumentNullException will be thrown if the inPredicate is null.

System.InvalidOperationException

An System.InvalidOperationException could be thrown if the collection is modified after the enumerator was created and before its use.

MaybeSingle<T>(IEnumerable<T>)

Returns the only element of a sequence, or an empty Maybe<T> if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

public static Maybe<T> MaybeSingle<T>(this IEnumerable<T> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

A collection in which to find a single member.

Returns

Maybe<T>

The single element of the input sequence, or an empty Maybe<T> if the sequence contains no elements.

Type Parameters

T

The type of the collection members.

MaybeSingle<T>(IEnumerable<T>, Func<T, Boolean>)

Returns the only element of a sequence that satisfies a specified condition, or an empty Maybe<T> if the sequence is empty; this method throws an exception if more than one such element exists.

public static Maybe<T> MaybeSingle<T>(this IEnumerable<T> inCollection, Func<T, bool> inPredicate)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

A collection in which to find a single member.

inPredicate System.Func<T, System.Boolean>

A function to test each element for a condition.

Returns

Maybe<T>

The single element of the input sequence that satisfies a condition, or an empty Maybe<T> if the sequence contains no elements.

Type Parameters

T

The type of the collection members.

Exceptions

System.ArgumentNullException

An System.ArgumentNullException will be thrown if the System.Func<TResult> inPredicate is null.

ThatExist<T>(IEnumerable<T>)

Returns a collection containing only the members of the input collection that have values.

public static IEnumerable<T> ThatExist<T>(this IEnumerable<T> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

The collection that may contain values.

Returns

System.Collections.Generic.IEnumerable<T>

A collection containing only the members of the input collection that have values.

Type Parameters

T

The type of members of the collection.

ThatExist<T>(IEnumerable<Maybe<T>>)

Returns a collection containing only the members of the input collection that have values.

public static IEnumerable<T> ThatExist<T>(this IEnumerable<Maybe<T>> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<Maybe<T>>

The collection that may contain values.

Returns

System.Collections.Generic.IEnumerable<T>

A collection containing only the members of the input collection that have values.

Type Parameters

T

The type of members of the collection.

ToCollectionOfMaybe<T>(IEnumerable<T>)

Converts a collection of T?? to a collection of Maybe<T> of the same values.

public static IEnumerable<Maybe<T>> ToCollectionOfMaybe<T>(this IEnumerable<T> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<T>

The collection containing the values to be converted to Maybe<T>.

Returns

System.Collections.Generic.IEnumerable<Maybe<T>>

A collection of Maybe<T> with the values in the original collection inCollection.

Type Parameters

T

The type of the values to be converted to a Maybe<T>.

WhereAllExist<T>(IEnumerable<Maybe<T>>)

Returns a collection only if all the members of the input collection have values.

public static Maybe<IEnumerable<T>> WhereAllExist<T>(this IEnumerable<Maybe<T>> inCollection)

Parameters

inCollection System.Collections.Generic.IEnumerable<Maybe<T>>

The collection that may contain values.

Returns

Maybe<System.Collections.Generic.IEnumerable<T>>

The input collection if all its members exist; otherwise no collection.

Type Parameters

T

The type of members of the collection.

See Also