Table of Contents

Class EnumerableEx

Namespace
Utils.Collections
Assembly
Utils.dll

Extension methods for working with IEnumerable<T> and related collections.

public static class EnumerableEx
Inheritance
EnumerableEx
Inherited Members

Methods

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

Flattens a sequence of sequences into a single, concatenated sequence.

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

Parameters

sources IEnumerable<IEnumerable<T>>

An IEnumerable{IEnumerable{T}} containing sub-sequences to flatten.

Returns

IEnumerable<T>

A single IEnumerable<T> containing all elements from the sub-sequences in order.

Type Parameters

T

The type of elements in the sub-sequences.

FollowedBy<T>(IEnumerable<T>, params IEnumerable<T>)

Enumerates all elements in enumerable followed by additional elements.

public static IEnumerable<T> FollowedBy<T>(this IEnumerable<T> enumerable, params IEnumerable<T> elements)

Parameters

enumerable IEnumerable<T>

The original sequence.

elements IEnumerable<T>

The elements to append.

Returns

IEnumerable<T>

A sequence that first yields every element of enumerable, and then yields every element of elements.

Type Parameters

T

The type of elements in the sequences.

GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)

Returns an element from a dictionary with the specific key or defaultValue if if doesn't exists

public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default)

Parameters

dictionary IDictionary<TKey, TValue>

Dictionary

key TKey

Key whose value to get

defaultValue TValue

Default value

Returns

TValue

Value of specific key or default value

Type Parameters

TKey

Key type

TValue

Value type

Exceptions

ArgumentNullException

Raise if dictionary is null

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

Indicates whether the enumerable has at least count elements.

public static bool HasAtLeastElements<T>(this IEnumerable<T> enumerable, int count)

Parameters

enumerable IEnumerable<T>

The sequence to check.

count int

The minimum number of elements required.

Returns

bool

true if the sequence has at least count elements; otherwise false.

Type Parameters

T

The type of elements in the sequence.

Exceptions

ArgumentNullException

Thrown if enumerable is null.

HasManyElements<T>(IEnumerable<T>)

Indicates whether the enumerable contains more than one element.

public static bool HasManyElements<T>(this IEnumerable<T> enumerable)

Parameters

enumerable IEnumerable<T>

The sequence to check.

Returns

bool

true if the sequence has more than one element; otherwise false.

Type Parameters

T

The type of elements in the sequence.

IsNullOrEmptyCollection<T>(IEnumerable<T>)

Returns true if collection is null or has no elements

public static bool IsNullOrEmptyCollection<T>(this IEnumerable<T> coll)

Parameters

coll IEnumerable<T>

Returns

bool

Type Parameters

T

Pack<T>(IEnumerable<T>)

Packs consecutive identical elements from source into groups, yielding a Pack<T>(IEnumerable<T>) for each group containing the value and its repetition count.

public static IEnumerable<Pack<T>> Pack<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The sequence of elements to pack.

Returns

IEnumerable<Pack<T>>

A sequence of Pack<T>(IEnumerable<T>) structures, each containing the element value and how many times it was repeated consecutively.

Type Parameters

T

The type of elements in the sequence.

Remarks

Uses Default for equality checks.

PrecededBy<T>(IEnumerable<T>, params IEnumerable<T>)

Enumerates the specified elements first, then enumerates all elements in enumerable.

public static IEnumerable<T> PrecededBy<T>(this IEnumerable<T> enumerable, params IEnumerable<T> elements)

Parameters

enumerable IEnumerable<T>

The original sequence.

elements IEnumerable<T>

The elements to prepend.

Returns

IEnumerable<T>

A sequence that first yields every element of elements, and then yields every element of enumerable.

Type Parameters

T

The type of elements in the sequences.

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

Scans the source for occurrences of the subsequence toReplace, and replaces them with the subsequence replacement. A naive, non-overlapping approach is used.

public static IEnumerable<T> Replace<T>(this IEnumerable<T> source, T[] toReplace, T[] replacement) where T : IEquatable<T>

Parameters

source IEnumerable<T>

The source sequence in which to perform replacements.

toReplace T[]

The subsequence to find and replace.

replacement T[]

The subsequence to substitute in place of toReplace.

Returns

IEnumerable<T>

A new sequence with the replacements applied where the pattern is matched.

Type Parameters

T

The element type; must implement IEquatable<T> for equality checks.

Remarks

Overlapping matches are not recognized. For example, if toReplace is [1, 2, 1], and the source is [1, 2, 1, 2, 1], this method only replaces the first match, ignoring the second (overlapping) match.

If toReplace is empty, no replacement is done and the original source is returned as-is.

Exceptions

ArgumentNullException

Thrown if any of the parameters (source, toReplace, or replacement) is null.

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

Returns a slice of source starting at skip and yielding at most take elements, using a single enumerator. This is equivalent to Skip(skip).Take(take) but avoids the two LINQ enumerator allocations that the chained form incurs.

public static IEnumerable<T> SkipTake<T>(this IEnumerable<T> source, int skip, int take)

Parameters

source IEnumerable<T>

The sequence to slice.

skip int

Number of elements to skip from the start.

take int

Maximum number of elements to return.

Returns

IEnumerable<T>

The requested slice of source.

Type Parameters

T

The type of elements in the sequence.

Exceptions

ArgumentNullException

Thrown if source is null.

Slice<T>(IEnumerable<T>, params IEnumerable<int>)

Slices the source into multiple segments at the specified cutIndexes.

public static IEnumerable<IEnumerable<T>> Slice<T>(this IEnumerable<T> source, params IEnumerable<int> cutIndexes)

Parameters

source IEnumerable<T>

The IEnumerable<T> to slice.

cutIndexes IEnumerable<int>

The indexes at which the sequence is sliced. They should be sorted in ascending order.

Returns

IEnumerable<IEnumerable<T>>

A sequence of segments (each itself an IEnumerable<T>), split at each index in cutIndexes.

Type Parameters

T

The type of elements in the sequence.

Remarks

If cutIndexes is empty, then the entire sequence is returned as a single segment.

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

Enumerates a sliding window of size windowSize over the source IEnumerable<T>. Each yielded array shares elements with the previous one in a sliding fashion.

public static IEnumerable<T[]> SlideEnumerateBy<T>(this IEnumerable<T> source, int windowSize, int skipCount = 0)

Parameters

source IEnumerable<T>

The sequence to read.

windowSize int

The size of each sliding window.

skipCount int

The number of elements to skip from the start before beginning.

Returns

IEnumerable<T[]>

An IEnumerable{Array{T}} where each array represents a window of size windowSize, except possibly the last one if there are not enough remaining elements.

Type Parameters

T

The element type of the source sequence.

Unpack<T>(IEnumerable<Pack<T>>)

Unpacks a sequence of Pack<T>(IEnumerable<T>) into a flat sequence, repeating each value according to its repetition count.

public static IEnumerable<T> Unpack<T>(this IEnumerable<Pack<T>> source)

Parameters

source IEnumerable<Pack<T>>

The packed sequence to unpack.

Returns

IEnumerable<T>

A flat IEnumerable<T> restoring the original sequence before packing.

Type Parameters

T

The type of the elements in the packed sequence.

Zip(bool, params IEnumerable[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<object[]> Zip(bool continueAfterShortestListEnds, params IEnumerable[] enumerations)

Parameters

continueAfterShortestListEnds bool

Continue when the shortest list ends

enumerations IEnumerable[]

IEnumerable to be read

Returns

IEnumerable<object[]>

Array of objects

Zip(bool, params (IEnumerable enumeration, object defaultValue)[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<object[]> Zip(bool continueAfterShortestListEnds, params (IEnumerable enumeration, object defaultValue)[] enumerations)

Parameters

continueAfterShortestListEnds bool

Continue when the shortest list ends

enumerations (IEnumerable enumeration, object defaultValue)[]

IEnumerable to be read

Returns

IEnumerable<object[]>

Array of objects

Zip(params IEnumerable[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<object[]> Zip(params IEnumerable[] enumerations)

Parameters

enumerations IEnumerable[]

IEnumerable to be read

Returns

IEnumerable<object[]>

Array of objects

Zip(params (IEnumerable enumeration, object defaultValue)[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<object[]> Zip(params (IEnumerable enumeration, object defaultValue)[] enumerations)

Parameters

enumerations (IEnumerable enumeration, object defaultValue)[]

IEnumerable to be read

Returns

IEnumerable<object[]>

Array of objects

Zip<TResult>(bool, Func<object[], TResult>, params IEnumerable[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<TResult> Zip<TResult>(bool continueAfterShortestListEnds, Func<object[], TResult> transform, params IEnumerable[] enumerations)

Parameters

continueAfterShortestListEnds bool

Continue when the shortest list ends

transform Func<object[], TResult>

Transform function

enumerations IEnumerable[]

IEnumerable to be read

Returns

IEnumerable<TResult>

Array of objects

Type Parameters

TResult

Zip<TResult>(bool, Func<object[], TResult>, params (IEnumerable enumeration, object defaultValue)[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<TResult> Zip<TResult>(bool continueAfterShortestListEnds, Func<object[], TResult> transform, params (IEnumerable enumeration, object defaultValue)[] enumerations)

Parameters

continueAfterShortestListEnds bool

Continue when the shortest list ends

transform Func<object[], TResult>

transformation function of input object to output object

enumerations (IEnumerable enumeration, object defaultValue)[]

IEnumerable to be read

Returns

IEnumerable<TResult>

Array of objects

Type Parameters

TResult

Zip<TResult>(Func<object[], TResult>, params IEnumerable[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<TResult> Zip<TResult>(Func<object[], TResult> transform, params IEnumerable[] enumerations)

Parameters

transform Func<object[], TResult>
enumerations IEnumerable[]

IEnumerable to be read

Returns

IEnumerable<TResult>

Array of objects

Type Parameters

TResult

Zip<TResult>(Func<object[], TResult>, params (IEnumerable enumeration, object defaultValue)[])

Read several IEnumerable in parallel and returns an array of object of elments from the same indexes

public static IEnumerable<TResult> Zip<TResult>(Func<object[], TResult> transform, params (IEnumerable enumeration, object defaultValue)[] enumerations)

Parameters

transform Func<object[], TResult>
enumerations (IEnumerable enumeration, object defaultValue)[]

IEnumerable to be read

Returns

IEnumerable<TResult>

Array of objects

Type Parameters

TResult

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

Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations

public static IEnumerable<(T1 Item1, T2 Item2)> Zip<T1, T2>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, T1 default1 = default, T2 default2 = default, bool continueAfterShortestListEnds = true)

Parameters

enumeration1 IEnumerable<T1>

Enumeration 1

enumeration2 IEnumerable<T2>

Enumeration 2

default1 T1

Default value after enumeration1 has end

default2 T2

Default value after enumeration2 has end

continueAfterShortestListEnds bool

Continue to enumerate the longuest enumeration when the shortest has end

Returns

IEnumerable<(T1 Key, T2 Value)>

Type Parameters

T1

Type for enumeration1 elements

T2

Type for enumeration2 elements

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

Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations

public static IEnumerable<(T1 Item1, T2 Item2, T3 Item3)> Zip<T1, T2, T3>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, IEnumerable<T3> enumeration3, T1 default1 = default, T2 default2 = default, T3 default3 = default, bool continueAfterShortestListEnds = true)

Parameters

enumeration1 IEnumerable<T1>

Enumeration 1

enumeration2 IEnumerable<T2>

Enumeration 2

enumeration3 IEnumerable<T3>

Enumeration 2

default1 T1

Default value after enumeration1 has end

default2 T2

Default value after enumeration2 has end

default3 T3

Default value after enumeration3 has end

continueAfterShortestListEnds bool

Continue to enumerate the longuest enumeration when the shortest has end

Returns

IEnumerable<(T1 Item1, T2 Item2, T3 Item3)>

Type Parameters

T1

Type for enumeration1 elements

T2

Type for enumeration2 elements

T3

Type for enumeration3 elements

Zip<T1, T2, TResult>(IEnumerable<T1>, IEnumerable<T2>, Func<T1, T2, TResult>, bool)

Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations

public static IEnumerable<TResult> Zip<T1, T2, TResult>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, Func<T1, T2, TResult> func, bool continueAfterShortestListEnds = true)

Parameters

enumeration1 IEnumerable<T1>

Enumeration 1

enumeration2 IEnumerable<T2>

Enumeration 2

func Func<T1, T2, TResult>

transformation function

continueAfterShortestListEnds bool

Continue to enumerate the longuest enumeration when the shortest has end

Returns

IEnumerable<TResult>

Type Parameters

T1

Type for enumeration1 elements

T2

Type for enumeration2 elements

TResult

Resulting type

Zip<T1, T2, T3, TResult>(IEnumerable<T1>, IEnumerable<T2>, IEnumerable<T3>, Func<T1, T2, T3, TResult>, bool)

Returns an enumerator with each element is the combination of the combined enumeration of enumeration1 and enumeration2 enumerations

public static IEnumerable<TResult> Zip<T1, T2, T3, TResult>(IEnumerable<T1> enumeration1, IEnumerable<T2> enumeration2, IEnumerable<T3> enumeration3, Func<T1, T2, T3, TResult> func, bool continueAfterShortestListEnds = true)

Parameters

enumeration1 IEnumerable<T1>

Enumeration 1

enumeration2 IEnumerable<T2>

Enumeration 2

enumeration3 IEnumerable<T3>

Enumeration 2

func Func<T1, T2, T3, TResult>

transformation function

continueAfterShortestListEnds bool

Continue to enumerate the longuest enumeration when the shortest has end

Returns

IEnumerable<TResult>

Type Parameters

T1

Type for enumeration1 elements

T2

Type for enumeration2 elements

T3

Type for enumeration3 elements

TResult

Type for resulting elements