Skip to content

IEnumerableExtensions

David Arno edited this page Feb 17, 2020 · 7 revisions

IEnumerable<T> Extension methods using Option<T>

Succinc<T> alternatives to FirstOrDefault, LastOrDefault, SingleOrDefault and ElementAtOrDefault


Introduction to Succinc<T>'s IEnumerable<T> extension methods

LINQ provides a set of methods that either return the value of an element of a collection, or return the type's default value if not. These methods suffer from two problems:

  1. If the type is a reference type, then the default value is null. This forces the developer into handling cases where the method returns null, preventing method chaining for example.
  2. If, for example, FirstOrDefault is called on a list of int values and 0 is returned, the developer has no way of knowing whether that's because the first element of the array contained 0, or because the list was empty.

Both cases are potential problems that the developer must guard against with defensive code: checking for null and testing the collection's size as well as requesting the element required.

Succinc<T> provides alternatives to these methods by returning an Option<T>. If the element was found, then a value is returned; otherwise None is returned. This addresses both of the above potential problems.

(For more details on interacting with the returned option, see the Option<T> page)

The extension methods supplied by Succinc<T> are:

  • TryElementAt
  • TryFirst
  • TryLast
  • TrySingle

TryElementAt

    public Option<T> IEnumerable<T>.TryElementAt(int index)

If the element at location index exists, Some<T> will be returned, containing the value at that location. If the collection is smaller than that required to access the element at location index, then None is returned.

TryFirst

Two versions of TryFirst are supported:

    public Option<T> IEnumerable<T>.TryFirst()

If the collection contains one or more elements, the value at index 0 is returned via Some<T>. Otherwise, None is returned.

    public Option<T> IEnumerable<T>.TryFirst(Func<T, bool> predicate)

For each element of the collection, predicate is invoked with the value of that element. If the predicate returns true, the enumeration of the collection stops and that value is returned via Some<T>. If the end of the collection is reached with no match, then None is returned.

TryLast

Two versions of TryLast are supported:

    public Option<T> IEnumerable<T>.TryLast()

If the collection contains one or more elements, the value at the last index of the collection is returned via Some<T>. Otherwise, None is returned.

    public Option<T> IEnumerable<T>.TryLast(Func<T, bool> predicate)

For every element of the collection, predicate is invoked with the value of that element. If the predicate returns true, that value is remembered (overriding any previous match) and the enumeration of the collection continues. If one or more matches occurred, the last remembered value is returned via Some<T>. If the end of the collection is reached with no match, then None is returned.

TrySingle

    public Option<T> IEnumerable<T>.TrySingle(Func<T, bool> predicate)

For each element of the collection, predicate is invoked with the value of that element. When the predicate first returns true, the value is remembered and the enumeration of the collection continues. If another predicate invocation then returns true, more than one match has occurred, so None is returned (there is no single element that matches the condition). If the end of the collection is reached with just one match, that value is returned via Some<T>. If no match occurred, then None is returned.