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
Similar to ElementAt<TSource>(IEnumerable<TSource>, int).
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
Similar to First<TSource>(IEnumerable<TSource>).
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
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
Similar to Last<TSource>(IEnumerable<TSource>).
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
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
Similar to Single<TSource>(IEnumerable<TSource>).
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
Exceptions
- ArgumentNullException
source
orpredicate
is null.