Class CollectionExtensions
- Namespace
- Badeend.Results.Extensions
- Assembly
- Badeend.Result.dll
Fallible overloads for commonly used collection operations.
public static class CollectionExtensions
- Inheritance
-
CollectionExtensions
- Inherited Members
Methods
TryElementAt<T>(IEnumerable<T>, int)
Get the element at the specified index
in the sequence.
Returns an error if the index exceeds the sequence length.
public static Result<T> TryElementAt<T>(this IEnumerable<T> source, int index)
Parameters
source
IEnumerable<T>index
int
Returns
- Result<T>
Type Parameters
T
Remarks
This method is similar to:
- ElementAt<TSource>(IEnumerable<TSource>, int), except that this method doesn't throw if the index exceeds the sequence length.
- ElementAtOrDefault<TSource>(IEnumerable<TSource>, int),
except that this method retains the distinction between: the index
was out of bounds, and: the index exists but happens to contains
the
default
value.
Exceptions
- ArgumentNullException
source
is null.- ArgumentOutOfRangeException
index
is negative.
TryFirst<T>(IEnumerable<T>)
Get the first element in the collection. Returns an error if the collection is empty.
public static Result<T> TryFirst<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>
Returns
- Result<T>
Type Parameters
T
Remarks
This method is similar to:
- First<TSource>(IEnumerable<TSource>), except that this method doesn't throw for empty collections.
- FirstOrDefault<TSource>(IEnumerable<TSource>),
except that this method retains the distinction between: an empty
collection, and: a non-empty collection whose first element happens
to be the
default
value.
Exceptions
- ArgumentNullException
source
is null.
TryFirst<T>(IEnumerable<T>, Func<T, bool>)
Get the first element in the collection that satisfies a specified
condition. Returns an error if no element in the collection satisfies
the condition in predicate
.
public static Result<T> TryFirst<T>(this IEnumerable<T> source, Func<T, bool> predicate)
Parameters
source
IEnumerable<T>predicate
Func<T, bool>
Returns
- Result<T>
Type Parameters
T
Remarks
This method is similar to:
- First<TSource>(IEnumerable<TSource>, Func<TSource, bool>), except that this method doesn't throw if there were no matches.
- FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource, bool>),
except that this method retains the distinction between: there were
no matches, and: there was a match but the element happens to be the
default
value.
Exceptions
- ArgumentNullException
source
orpredicate
is null.
TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)
Get the value associated with the specified key
.
Returns an error if the key was not found.
public static Result<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
Parameters
dictionary
IReadOnlyDictionary<TKey, TValue>key
TKey
Returns
- Result<TValue>
Type Parameters
TKey
TValue
Exceptions
- ArgumentNullException
dictionary
is null.
TryIndexOf<T>(IReadOnlyList<T>, T)
Find the index of the first occurrence of item
in
the list. Returns an error if not found.
public static Result<int> TryIndexOf<T>(this IReadOnlyList<T> list, T item)
Parameters
list
IReadOnlyList<T>item
T
Returns
Type Parameters
T
Remarks
Similar to IndexOf(T), but with the added guarantee that the returned integer is always a valid index (i.e. not negative).
Exceptions
- ArgumentNullException
list
is null.
TryLastIndexOf<T>(IReadOnlyList<T>, T)
Find the index of the last occurrence of item
in
the list. Returns an error if not found.
public static Result<int> TryLastIndexOf<T>(this IReadOnlyList<T> list, T item)
Parameters
list
IReadOnlyList<T>item
T
Returns
Type Parameters
T
Remarks
Similar to LastIndexOf(T), but with the added guarantee that the returned integer is always a valid index (i.e. not negative).
Exceptions
- ArgumentNullException
list
is null.
TryLast<T>(IEnumerable<T>)
Get the last element in the collection. Returns an error if the collection is empty.
public static Result<T> TryLast<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>
Returns
- Result<T>
Type Parameters
T
Remarks
This method is similar to:
- Last<TSource>(IEnumerable<TSource>), except that this method doesn't throw for empty collections.
- LastOrDefault<TSource>(IEnumerable<TSource>),
except that this method retains the distinction between: an empty
collection, and: a non-empty collection whose last element happens
to be the
default
value.
Exceptions
- ArgumentNullException
source
is null.
TryLast<T>(IEnumerable<T>, Func<T, bool>)
Get the last element in the collection that satisfies a specified
condition. Returns an error if no element in the collection satisfies
the condition in predicate
.
public static Result<T> TryLast<T>(this IEnumerable<T> source, Func<T, bool> predicate)
Parameters
source
IEnumerable<T>predicate
Func<T, bool>
Returns
- Result<T>
Type Parameters
T
Remarks
This method is similar to:
- Last<TSource>(IEnumerable<TSource>, Func<TSource, bool>), except that this method doesn't throw if there were no matches.
- LastOrDefault<TSource>(IEnumerable<TSource>, Func<TSource, bool>),
except that this method retains the distinction between: there were
no matches, and: there was a match but the element happens to be the
default
value.
Exceptions
- ArgumentNullException
source
orpredicate
is null.
TryMax<T>(IEnumerable<T>)
Get the maximum value in the sequence. Returns an error if the sequence does not contain any non-null elements.
public static Result<T> TryMax<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>
Returns
- Result<T>
Type Parameters
T
Remarks
Similar to Max<TSource>(IEnumerable<TSource>).
Exceptions
- ArgumentNullException
source
is null.
TryMin<T>(IEnumerable<T>)
Get the minimum value in the sequence. Returns an error if the sequence does not contain any non-null elements.
public static Result<T> TryMin<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>
Returns
- Result<T>
Type Parameters
T
Remarks
Similar to Min<TSource>(IEnumerable<TSource>).
Exceptions
- ArgumentNullException
source
is null.
TryRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey)
Remove the value with the specified key
from the dictionary
.
Returns the removed value or an error if the key was not found.
public static Result<TValue> TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
dictionary
IDictionary<TKey, TValue>key
TKey
Returns
- Result<TValue>
Type Parameters
TKey
TValue
Exceptions
- ArgumentNullException
dictionary
is null.
TrySingle<T>(IEnumerable<T>)
Get the only element in the collection. Returns an error if there is not exactly one element in the collection.
public static Result<T, TrySingleError> TrySingle<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>
Returns
- Result<T, TrySingleError>
Type Parameters
T
Remarks
This method is similar to:
- Single<TSource>(IEnumerable<TSource>), except that this method doesn't throw for wrongly-sized collections.
- SingleOrDefault<TSource>(IEnumerable<TSource>),
except that this method retains the distinction between:
the collection doesn't contain a single element, and:
the collection has exactly one element that happens to be the
default
value.
Exceptions
- ArgumentNullException
source
is null.
TrySingle<T>(IEnumerable<T>, Func<T, bool>)
Get the only element in the collection that satisfies a specified
condition. Returns an error if there is not exactly one element
matching the predicate
.
public static Result<T, TrySingleError> TrySingle<T>(this IEnumerable<T> source, Func<T, bool> predicate)
Parameters
source
IEnumerable<T>predicate
Func<T, bool>
Returns
- Result<T, TrySingleError>
Type Parameters
T
Remarks
This method is similar to:
- Single<TSource>(IEnumerable<TSource>, Func<TSource, bool>), except that this method doesn't throw if there wasn't exactly one match.
- SingleOrDefault<TSource>(IEnumerable<TSource>, Func<TSource, bool>),
except that this method retains the distinction between: there wasn't
exactly one match, and: there was a single match but the element
happens to be the
default
value.
Exceptions
- ArgumentNullException
source
orpredicate
is null.