Table of Contents

Class IEnumerableExtensions

Extension methods for IEnumerable<T>.

public static class IEnumerableExtensions
Inheritance
IEnumerableExtensions
Inherited Members

Methods

AreAllDistinct<T>(IEnumerable<T>)

Returns whether all the elements in the IEnumerable<T> are distinct. Uses the default equality comparer for type T.

public static bool AreAllDistinct<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

bool

Type Parameters

T

ArgMax<TElement, TCompare>(IEnumerable<TElement>, Func<TElement, TCompare>)

Applies the function to each element and returns the element that gives the highest output. If multiple elements give the highest output, the first one will be returned.

public static TElement ArgMax<TElement, TCompare>(this IEnumerable<TElement> elements, Func<TElement, TCompare> function) where TCompare : IComparable<TCompare>

Parameters

elements IEnumerable<TElement>
function Func<TElement, TCompare>

Returns

TElement

Type Parameters

TElement
TCompare

ArgMin<TElement, TCompare>(IEnumerable<TElement>, Func<TElement, TCompare>)

Applies the function to each element and returns the element that gives the lowest output. If multiple elements give the lowest output, the first one will be returned.

public static TElement ArgMin<TElement, TCompare>(this IEnumerable<TElement> elements, Func<TElement, TCompare> function) where TCompare : IComparable<TCompare>

Parameters

elements IEnumerable<TElement>
function Func<TElement, TCompare>

Returns

TElement

Type Parameters

TElement
TCompare

Concat<T>(IEnumerable<T>, IEnumerable<T>, IEnumerable<T>, params IEnumerable<T>[])

Creates a new IEnumerable<T> that iterates through all of this one, then all of second, etc. Extends Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) to allow concating multiple IEnumerable<T>s.

public static IEnumerable<T> Concat<T>(this IEnumerable<T> first, IEnumerable<T> second, IEnumerable<T> third, params IEnumerable<T>[] subsequent)

Parameters

first IEnumerable<T>
second IEnumerable<T>
third IEnumerable<T>
subsequent IEnumerable<T>[]

Returns

IEnumerable<T>

Type Parameters

T

ContainsAll<T>(IEnumerable<T>, IEnumerable<T>)

Whether all the given elements are in the ICollection<T>.

public static bool ContainsAll<T>(this IEnumerable<T> iEnumerable, IEnumerable<T> elements)

Parameters

iEnumerable IEnumerable<T>
elements IEnumerable<T>

Returns

bool

Type Parameters

T

ContainsAll<T>(IEnumerable<T>, params T[])

Whether all the given elements are in the IEnumerable<T>.

public static bool ContainsAll<T>(this IEnumerable<T> iEnumerable, params T[] elements)

Parameters

iEnumerable IEnumerable<T>
elements T[]

Returns

bool

Type Parameters

T

ContainsAny<T>(IEnumerable<T>, IEnumerable<T>)

Whether any of the given elements are in the IEnumerable<T>.

public static bool ContainsAny<T>(this IEnumerable<T> iEnumerable, IEnumerable<T> elements)

Parameters

iEnumerable IEnumerable<T>
elements IEnumerable<T>

Returns

bool

Type Parameters

T

ContainsAny<T>(IEnumerable<T>, params T[])

Whether any of the given elements are in the IEnumerable<T>.

public static bool ContainsAny<T>(this IEnumerable<T> iEnumerable, params T[] elements)

Parameters

iEnumerable IEnumerable<T>
elements T[]

Returns

bool

Type Parameters

T

ContainsNone<T>(IEnumerable<T>, IEnumerable<T>)

Whether none of the given elements are in the IEnumerable<T>.

public static bool ContainsNone<T>(this IEnumerable<T> iEnumerable, IEnumerable<T> elements)

Parameters

iEnumerable IEnumerable<T>
elements IEnumerable<T>

Returns

bool

Type Parameters

T

ContainsNone<T>(IEnumerable<T>, params T[])

Whether none of the given elements are in the IEnumerable<T>.

public static bool ContainsNone<T>(this IEnumerable<T> iEnumerable, params T[] elements)

Parameters

iEnumerable IEnumerable<T>
elements T[]

Returns

bool

Type Parameters

T

ContainsNotAll<T>(IEnumerable<T>, IEnumerable<T>)

Whether at least one of the given elements is not in the ICollection<T>.

public static bool ContainsNotAll<T>(this IEnumerable<T> iEnumerable, IEnumerable<T> elements)

Parameters

iEnumerable IEnumerable<T>
elements IEnumerable<T>

Returns

bool

Type Parameters

T

ContainsNotAll<T>(IEnumerable<T>, params T[])

Whether at least one of the given elements is not in the IEnumerable<T>.

public static bool ContainsNotAll<T>(this IEnumerable<T> iEnumerable, params T[] elements)

Parameters

iEnumerable IEnumerable<T>
elements T[]

Returns

bool

Type Parameters

T

CountIsAtLeast<T>(IEnumerable<T>, int)

Returns whether the given IEnumerable<T> has >= n elements.

public static bool CountIsAtLeast<T>(this IEnumerable<T> elements, int n)

Parameters

elements IEnumerable<T>
n int

Returns

bool

Type Parameters

T

Remarks

Iterates through at most n elements of the IEnumerable<T>.

CountIsAtMost<T>(IEnumerable<T>, int)

Returns whether the given IEnumerable<T> has <= n elements.

public static bool CountIsAtMost<T>(this IEnumerable<T> elements, int n)

Parameters

elements IEnumerable<T>
n int

Returns

bool

Type Parameters

T

Remarks

Iterates through at most n + 1 elements of the IEnumerable<T>.

CountIsExactly<T>(IEnumerable<T>, int)

Returns whether the given IEnumerable<T> has exactly n elements.

public static bool CountIsExactly<T>(this IEnumerable<T> elements, int n)

Parameters

elements IEnumerable<T>
n int

Returns

bool

Type Parameters

T

Remarks

Iterates through at most n + 1 elements of the IEnumerable<T>.

EnumerateOccurrences<T>(IEnumerable<T>)

Pairs each element with the number of times that element has already occurred.

char[] actual = new char[] { 'a', 'b', 'a', 'c', 'b', 'a' }.EnumerateOccurrences();
char[] expected = new (char, int)[] { ('a', 0), ('b', 0), ('a', 1), ('c', 0), ('b', 1), ('a', 2) };
expected.SequenceEqual(actual);  // true
public static IEnumerable<(T element, int occurrence)> EnumerateOccurrences<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

IEnumerable<(T element, int index)>

Type Parameters

T

Remarks

Uses the default equality comparer for type T.

Enumerate<T>(IEnumerable<T>)

Returns (elements[0], 0), (elements[1], 1), ..., (elements[^1], elements.Count - 1).

public static IEnumerable<(T element, int index)> Enumerate<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

IEnumerable<(T element, int index)>

Type Parameters

T

Flatten<T>(IEnumerable<IEnumerable<T>>)

Flattens a sequence of sequences into one sequence, by first iterating over the first sequence, then the second sequence, etc. For example, { { 1, 2, 3 }, { 4, 5 }, { }, { 6, 7, 8, 9, 10 } } flattens to { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }.

public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> sequences)

Parameters

sequences IEnumerable<IEnumerable<T>>

Returns

IEnumerable<T>

Type Parameters

T

Exceptions

ArgumentNullException

sequences is null.

ArgumentException

One of the sequences in sequences is null.

IsSubsequenceOf<T>(IEnumerable<T>, IEnumerable<T>)

Returns whether this IEnumerable<T> is a subsequence of otherSequence; that is, otherSequence is this IEnumerable<T> with potentially extra elements added anywhere in the sequence.

public static bool IsSubsequenceOf<T>(this IEnumerable<T> sequence, IEnumerable<T> otherSequence)

Parameters

sequence IEnumerable<T>
otherSequence IEnumerable<T>

Returns

bool

Type Parameters

T
See Also

IsSupersequenceOf<T>(IEnumerable<T>, IEnumerable<T>)

Returns whether this IEnumerable<T> is a supersequence of otherSequence; that is, this IEnumerable<T> is otherSequence with potentially extra elements added anywhere in the sequence.

public static bool IsSupersequenceOf<T>(this IEnumerable<T> sequence, IEnumerable<T> otherSequence)

Parameters

sequence IEnumerable<T>
otherSequence IEnumerable<T>

Returns

bool

Type Parameters

T
See Also

MinAndMax<T>(IEnumerable<T>)

Returns the minimum and maximum element of the IEnumerable<T>. Can be more efficient than calling Min<TSource>(IEnumerable<TSource>) and Max<TSource>(IEnumerable<TSource>) as this only iterates through the IEnumerable<T> once.

Throws an exception if T doesn't have a default comparer.

public static (T min, T max) MinAndMax<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

(T, T)

Type Parameters

T

None<T>(IEnumerable<T>)

Returns true iff the given sequence is empty.

public static bool None<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

bool

Type Parameters

T

None<T>(IEnumerable<T>, Func<T, bool>)

Returns true iff none of the elements satisfy the predicate.

public static bool None<T>(this IEnumerable<T> elements, Func<T, bool> predicate)

Parameters

elements IEnumerable<T>
predicate Func<T, bool>

Returns

bool

Type Parameters

T

NotAll<T>(IEnumerable<T>, Func<T, bool>)

Returns true iff at least one of the elements doesn't satisfy the predicate.

public static bool NotAll<T>(this IEnumerable<T> elements, Func<T, bool> predicate)

Parameters

elements IEnumerable<T>
predicate Func<T, bool>

Returns

bool

Type Parameters

T

Product(IEnumerable<decimal>)

Returns the product of all elements in the sequence, or 1 if the sequence is empty.

public static decimal Product(this IEnumerable<decimal> elements)

Parameters

elements IEnumerable<decimal>

Returns

decimal

Product(IEnumerable<double>)

Returns the product of all elements in the sequence, or 1 if the sequence is empty.

public static double Product(this IEnumerable<double> elements)

Parameters

elements IEnumerable<double>

Returns

double

Product(IEnumerable<int>)

Returns the product of all elements in the sequence, or 1 if the sequence is empty.

public static int Product(this IEnumerable<int> elements)

Parameters

elements IEnumerable<int>

Returns

int

Product(IEnumerable<long>)

Returns the product of all elements in the sequence, or 1 if the sequence is empty.

public static long Product(this IEnumerable<long> elements)

Parameters

elements IEnumerable<long>

Returns

long

Product(IEnumerable<float>)

Returns the product of all elements in the sequence, or 1 if the sequence is empty.

public static float Product(this IEnumerable<float> elements)

Parameters

elements IEnumerable<float>

Returns

float

RepeatIEnumerable<T>(IEnumerable<T>)

Creates an IEnumerable<T> that repeats the given IEnumerable<T> indefinitely, looping back round to the beginning when it reaches the end.

public static IEnumerable<T> RepeatIEnumerable<T>(IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

IEnumerable<T>

Type Parameters

T

Repeat<T>(T)

Creates an IEnumerable<T> that repeats the given element indefinitely.

public static IEnumerable<T> Repeat<T>(T element)

Parameters

element T

Returns

IEnumerable<T>

Type Parameters

T

Replace<T>(IEnumerable<T>, T, T)

Replaces all occurrences of toReplace with replaceWith. Uses the default equality comparer for type T.

public static IEnumerable<T> Replace<T>(this IEnumerable<T> elements, T toReplace, T replaceWith)

Parameters

elements IEnumerable<T>
toReplace T
replaceWith T

Returns

IEnumerable<T>

Type Parameters

T

Singleton<T>(T)

Creates an IEnumerable<T> with only one element.

public static IEnumerable<T> Singleton<T>(T element)

Parameters

element T

Returns

IEnumerable<T>

Type Parameters

T

SkipIndex<T>(IEnumerable<T>, int)

Returns the given sequence but without the element at the given index. If the index is outside the range of the sequence, the sequence will be unaffected.

public static IEnumerable<T> SkipIndex<T>(this IEnumerable<T> elements, int index)

Parameters

elements IEnumerable<T>
index int

Returns

IEnumerable<T>

Type Parameters

T

ToPrettyString<T>(IEnumerable<T>)

Formats elements into a string of the form "{ elements[0], ..., elements[^1] }" (with the '...' replaced by elements).

public static string ToPrettyString<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

string

Type Parameters

T

ZipCurrentAndNext<T>(IEnumerable<T>)

Returns (elements[0], elements[1]), (elements[1], elements[2]), ..., (elements[^2], elements[^1]).

Returns an empty IEnumerable<T> if elements has length 0 or 1.

public static IEnumerable<(T current, T next)> ZipCurrentAndNext<T>(this IEnumerable<T> elements)

Parameters

elements IEnumerable<T>

Returns

IEnumerable<(T current, T next)>

Type Parameters

T

Zip<T1, T2>(IEnumerable<T1>, IEnumerable<T2>)

Returns (left[0], right[0]), (left[1], right[1]), ..., until either left or right ends.

public static IEnumerable<(T1 left, T2 right)> Zip<T1, T2>(this IEnumerable<T1> left, IEnumerable<T2> right)

Parameters

left IEnumerable<T1>
right IEnumerable<T2>

Returns

IEnumerable<(T1 left, T2 right)>

Type Parameters

T1
T2