Table of Contents

Class MaybeCollectionExtensions

Namespace
Trellis
Assembly
Trellis.Results.dll

Extension methods for safely querying collections, returning Maybe<T> instead of throwing.

public static class MaybeCollectionExtensions
Inheritance
MaybeCollectionExtensions
Inherited Members

Methods

TryFirst<T>(IEnumerable<T>)

Returns the first element of a sequence wrapped in a Maybe<T>, or None if the sequence contains no elements.

public static Maybe<T> TryFirst<T>(this IEnumerable<T> source) where T : notnull

Parameters

source IEnumerable<T>

The sequence to return the first element of.

Returns

Maybe<T>

A Maybe<T> containing the first element, or None if the sequence is empty.

Type Parameters

T

The type of the elements of source.

Exceptions

ArgumentNullException

Thrown when source is null.

TryFirst<T>(IEnumerable<T>, Func<T, bool>)

Returns the first element of a sequence that satisfies a condition wrapped in a Maybe<T>, or None if no such element is found.

public static Maybe<T> TryFirst<T>(this IEnumerable<T> source, Func<T, bool> predicate) where T : notnull

Parameters

source IEnumerable<T>

The sequence to search.

predicate Func<T, bool>

A function to test each element for a condition.

Returns

Maybe<T>

A Maybe<T> containing the first matching element, or None if no match is found.

Type Parameters

T

The type of the elements of source.

Exceptions

ArgumentNullException

Thrown when source or predicate is null.

TryLast<T>(IEnumerable<T>)

Returns the last element of a sequence wrapped in a Maybe<T>, or None if the sequence contains no elements.

public static Maybe<T> TryLast<T>(this IEnumerable<T> source) where T : notnull

Parameters

source IEnumerable<T>

The sequence to return the last element of.

Returns

Maybe<T>

A Maybe<T> containing the last element, or None if the sequence is empty.

Type Parameters

T

The type of the elements of source.

Exceptions

ArgumentNullException

Thrown when source is null.

TryLast<T>(IEnumerable<T>, Func<T, bool>)

Returns the last element of a sequence that satisfies a condition wrapped in a Maybe<T>, or None if no such element is found.

public static Maybe<T> TryLast<T>(this IEnumerable<T> source, Func<T, bool> predicate) where T : notnull

Parameters

source IEnumerable<T>

The sequence to search.

predicate Func<T, bool>

A function to test each element for a condition.

Returns

Maybe<T>

A Maybe<T> containing the last matching element, or None if no match is found.

Type Parameters

T

The type of the elements of source.

Exceptions

ArgumentNullException

Thrown when source or predicate is null.