Class ExtensionMethodsEnumerable
Provides a set of extension methods dedicated to enumerables.
public static class ExtensionMethodsEnumerable
- Inheritance
-
ExtensionMethodsEnumerable
- Inherited Members
Methods (19)
Append<T>(IEnumerable<T>, T)
Append element at the end of seq.
public static IEnumerable<T> Append<T>(IEnumerable<T> seq, T element)
Parameters
seqIEnumerable<T>A sequence of elements.
elementTThe element to append.
Returns
- IEnumerable<T>
Type Parameters
TThe element type.
Remarks
This extension method has a constant time complexity.
Before v2021.2 this method used to be an extension method but with v2021.2 NDepend compiles to net472.
Thus this method collided with the Enumerable.Append() introduced within net471.
Some default rules used this method in a non-extension way so we made it as a non-extension method.
Append<T>(IEnumerable<T>, T, T)
Append element1 and element1 at the end of seq.
public static IEnumerable<T> Append<T>(this IEnumerable<T> seq, T element1, T element2)
Parameters
seqIEnumerable<T>A sequence of elements.
element1TThe element to append first.
element2TThe element to append after
element1.
Returns
- IEnumerable<T>
Type Parameters
TThe element type.
Remarks
This extension method has a constant time complexity.
Append<T>(IEnumerable<T>, T, T, T)
Append element1, element2 and element3 at the end of seq.
public static IEnumerable<T> Append<T>(this IEnumerable<T> seq, T element1, T element2, T element3)
Parameters
seqIEnumerable<T>A sequence of elements.
element1TThe element to append first.
element2TThe element to append after
element1.element3TThe element to append after
element2.
Returns
- IEnumerable<T>
Type Parameters
TThe element type.
Remarks
To append more than 3 elements to seq, use the .Concat(new [] { a, b, c, d ... } ) syntax.
Concat<TElement>(IEnumerable<TElement>, TElement)
Gets an enumerable object that contains the elements of elements and then element, in this order.
public static IEnumerable<TElement> Concat<TElement>(this IEnumerable<TElement> elements, TElement element)
Parameters
elementsIEnumerable<TElement>The first elements in the returned enumerable.
elementTElementThe last element in the returned enumerable.
Returns
- IEnumerable<TElement>
Type Parameters
TElementThe type of
element.
Concat<TElement>(TElement, IEnumerable<TElement>)
Gets an enumerable object that contains first element and then elements of elements, in this order.
public static IEnumerable<TElement> Concat<TElement>(this TElement element, IEnumerable<TElement> elements)
Parameters
elementTElementThe first element in the returned enumerable.
elementsIEnumerable<TElement>The last elements in the returned enumerable.
Returns
- IEnumerable<TElement>
Type Parameters
TElementThe type of
element.
ContainsAll<T>(IEnumerable<T>, IEnumerable<T>)
Determine if superset contains all elements of subset.
public static bool ContainsAll<T>(this IEnumerable<T> superset, IEnumerable<T> subset)
Parameters
supersetIEnumerable<T>This set that might contains the subset.
subsetIEnumerable<T>The set to test if contained in superset.
Returns
Type Parameters
TThe type parameter of the items in the sets.
Remarks
true is returned if subset is empty.
true is returned if both subset and superset are empty.
false is returned if superset is empty and subset is not empty.
ContainsNullElement<T>(IEnumerable<T>)
Returns true if the sequence seq contains a null element.
public static bool ContainsNullElement<T>(this IEnumerable<T> seq) where T : class
Parameters
seqIEnumerable<T>The sequence that contains items tested for nullity.
Returns
Type Parameters
T
Distinct<T, TKey>(IEnumerable<T>, Func<T, TKey>)
Returns distinct elements from a sequence seq by using the default equality comparer of the type TKey, on which items are projected through funcProjection.
public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> seq, Func<T, TKey> funcProjection)
Parameters
seqIEnumerable<T>The source sequence that contains elements.
funcProjectionFunc<T, TKey>The projection function to obtain an instance of type
TKeyfrom an instance of typeT.
Returns
- IEnumerable<T>
The sequence of distinct items.
Type Parameters
TThe type of the elements of
seq.TKeyThe type on which elements of
seqare projected.
Distinct<T, TKey>(IEnumerable<T>, Func<T, TKey>, IEqualityComparer<TKey>)
Returns distinct elements from a sequence seq by using the default equality comparer of the type TKey, on which items are projected through funcProjection.
public static IEnumerable<T> Distinct<T, TKey>(this IEnumerable<T> seq, Func<T, TKey> funcProjection, IEqualityComparer<TKey> comparer)
Parameters
seqIEnumerable<T>The source sequence that contains elements.
funcProjectionFunc<T, TKey>The projection function to obtain an instance of type
TKeyfrom an instance of typeT.comparerIEqualityComparer<TKey>Equality comparer on
TKey.
Returns
- IEnumerable<T>
The sequence of distinct items.
Type Parameters
TThe type of the elements of
seq.TKeyThe type on which elements of
seqare projected.
Except<T>(IEnumerable<T>, T)
Produces the set seq excluding elementExcluded. The equality test relies on the Equals() method.
public static IEnumerable<T> Except<T>(this IEnumerable<T> seq, T elementExcluded)
Parameters
seqIEnumerable<T>A sequence of elements.
elementExcludedTThe element excluded.
Returns
- IEnumerable<T>
A sequence that contains the set difference of the elements of
seqminuselementExcluded.
Type Parameters
TThe element type.
Remarks
This extension method has a constant time complexity.
FirstOrAnyOrDefault<T>(IEnumerable<T>, Func<T, bool>)
Returns the first element of a sequence satisfying predicate, or the first element of the sequence, or a default value if no element is found.
public static T FirstOrAnyOrDefault<T>(this IEnumerable<T> seq, Func<T, bool> predicate)
Parameters
seqIEnumerable<T>A sequence of elements.
predicateFunc<T, bool>A predicate on elements
Returns
- T
Type Parameters
TThe element type.
IndexOf<T>(IReadOnlyList<T>, T)
Determines the index of a specific item in readOnlyList.
public static int IndexOf<T>(this IReadOnlyList<T> readOnlyList, T value)
Parameters
readOnlyListIReadOnlyList<T>valueTThe object to locate in
readOnlyList.
Returns
- int
The index of value if found in the list; otherwise, -1.
Type Parameters
TThe type parameter of the items in the read-only list.
Remarks
This method uses the EqualityComparer$lt;T>.Default.Equals() method on value to determine whether item exists
ToEnumerable<TElement>(TElement)
Gets an enumerable object that contains only the single element element.
public static IEnumerable<TElement> ToEnumerable<TElement>(this TElement element)
Parameters
elementTElementThe single element in the returned enumerable.
Returns
- IEnumerable<TElement>
Type Parameters
TElementThe type of
element.
ToMultiKeyLookup<T, TKey>(IEnumerable<T>, Func<T, IEnumerable<TKey>>)
Creates a Lookup<TKey, TElement> from an IEnumerable<T> according to a specified key selector function. The funtion can return zero, one or several keys for an element..
public static ILookup<TKey, T> ToMultiKeyLookup<T, TKey>(this IEnumerable<T> seq, Func<T, IEnumerable<TKey>> func)
Parameters
seqIEnumerable<T>An IEnumerable<T> to create an hashset from.
funcFunc<T, IEnumerable<TKey>>A function to extract a sequence of keys from each element.
Returns
- ILookup<TKey, T>
An hashset that contains the elements from the input sequence.
Type Parameters
TThe type of the elements of
seq.TKeyThe type of the keys in the result Lookup<TKey, TElement>.
Remarks
The difference with ToLookup<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>) is that an element can have zero, one or several keys.
Hence an element can be contained in several groups, if it has several keys.
- See Also
ToReadOnlyClonedCollection<T>(IEnumerable<T>)
Creates a IReadOnlyCollection<T> cloned collection around sequence.
public static IReadOnlyCollection<T> ToReadOnlyClonedCollection<T>(this IEnumerable<T> sequence)
Parameters
sequenceIEnumerable<T>A collection object.
Returns
Type Parameters
TThe type parameter of the items in the collection.
ToReadOnlyClonedList<T>(IEnumerable<T>)
Creates a IReadOnlyCollection<T> cloned collection around sequence.
public static IReadOnlyList<T> ToReadOnlyClonedList<T>(this IEnumerable<T> sequence)
Parameters
sequenceIEnumerable<T>A list object.
Returns
Type Parameters
TThe type parameter of the items in the list.
ToReadOnlyWrappedCollection<T>(ICollection<T>)
Creates a IReadOnlyCollection<T> wrapper collection around collection.
public static IReadOnlyCollection<T> ToReadOnlyWrappedCollection<T>(this ICollection<T> collection)
Parameters
collectionICollection<T>A collection object.
Returns
Type Parameters
TThe type parameter of the items in the collection.
ToReadOnlyWrappedList<T>(IList<T>)
Creates a IReadOnlyCollection<T> wrapper collection around list.
public static IReadOnlyList<T> ToReadOnlyWrappedList<T>(this IList<T> list)
Parameters
listIList<T>A list object.
Returns
Type Parameters
TThe type parameter of the items in the list.
TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)
Add a pair of key and value to dico, only if dico doesn't already contain the key.
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dico, TKey key, TValue value)
Parameters
dicoIDictionary<TKey, TValue>This dictionary.
keyTKeyThe key.
valueTValueThe value.
Returns
- bool
false if the
dicoalready contains thekey. Else add the pairkeyandvalueand returns true.
Type Parameters
TKeyTValue