Table of Contents

Class ExtensionMethodsEnumerable

Namespace
NDepend.Helpers
Assembly
NDepend.API.dll

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

seq IEnumerable<T>

A sequence of elements.

element T

The element to append.

Returns

IEnumerable<T>

Type Parameters

T

The 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

seq IEnumerable<T>

A sequence of elements.

element1 T

The element to append first.

element2 T

The element to append after element1.

Returns

IEnumerable<T>

Type Parameters

T

The 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

seq IEnumerable<T>

A sequence of elements.

element1 T

The element to append first.

element2 T

The element to append after element1.

element3 T

The element to append after element2.

Returns

IEnumerable<T>

Type Parameters

T

The 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

elements IEnumerable<TElement>

The first elements in the returned enumerable.

element TElement

The last element in the returned enumerable.

Returns

IEnumerable<TElement>

Type Parameters

TElement

The 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

element TElement

The first element in the returned enumerable.

elements IEnumerable<TElement>

The last elements in the returned enumerable.

Returns

IEnumerable<TElement>

Type Parameters

TElement

The 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

superset IEnumerable<T>

This set that might contains the subset.

subset IEnumerable<T>

The set to test if contained in superset.

Returns

bool

Type Parameters

T

The 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

seq IEnumerable<T>

The sequence that contains items tested for nullity.

Returns

bool

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

seq IEnumerable<T>

The source sequence that contains elements.

funcProjection Func<T, TKey>

The projection function to obtain an instance of type TKey from an instance of type T.

Returns

IEnumerable<T>

The sequence of distinct items.

Type Parameters

T

The type of the elements of seq.

TKey

The type on which elements of seq are 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

seq IEnumerable<T>

The source sequence that contains elements.

funcProjection Func<T, TKey>

The projection function to obtain an instance of type TKey from an instance of type T.

comparer IEqualityComparer<TKey>

Equality comparer on TKey.

Returns

IEnumerable<T>

The sequence of distinct items.

Type Parameters

T

The type of the elements of seq.

TKey

The type on which elements of seq are 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

seq IEnumerable<T>

A sequence of elements.

elementExcluded T

The element excluded.

Returns

IEnumerable<T>

A sequence that contains the set difference of the elements of seq minus elementExcluded.

Type Parameters

T

The 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

seq IEnumerable<T>

A sequence of elements.

predicate Func<T, bool>

A predicate on elements

Returns

T

Type Parameters

T

The 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

readOnlyList IReadOnlyList<T>
value T

The object to locate in readOnlyList.

Returns

int

The index of value if found in the list; otherwise, -1.

Type Parameters

T

The 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

element TElement

The single element in the returned enumerable.

Returns

IEnumerable<TElement>

Type Parameters

TElement

The 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

seq IEnumerable<T>

An IEnumerable<T> to create an hashset from.

func Func<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

T

The type of the elements of seq.

TKey

The 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
ToLookup<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

ToReadOnlyClonedCollection<T>(IEnumerable<T>)

Creates a IReadOnlyCollection<T> cloned collection around sequence.

public static IReadOnlyCollection<T> ToReadOnlyClonedCollection<T>(this IEnumerable<T> sequence)

Parameters

sequence IEnumerable<T>

A collection object.

Returns

IReadOnlyCollection<T>

Type Parameters

T

The 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

sequence IEnumerable<T>

A list object.

Returns

IReadOnlyList<T>

Type Parameters

T

The 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

collection ICollection<T>

A collection object.

Returns

IReadOnlyCollection<T>

Type Parameters

T

The 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

list IList<T>

A list object.

Returns

IReadOnlyList<T>

Type Parameters

T

The 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

dico IDictionary<TKey, TValue>

This dictionary.

key TKey

The key.

value TValue

The value.

Returns

bool

false if the dico already contains the key. Else add the pair key and value and returns true.

Type Parameters

TKey
TValue