Show / Hide Table of Contents

Class Algorithms

Algorithms contains a number of static methods that implement algorithms that work on collections. Most of the methods deal with the standard generic collection interfaces such as IEnumerable<T>, ICollection<T> and IList<T>.

Inheritance
System.Object
Algorithms
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
public static class Algorithms

Methods

| Improve this Doc View Source

BinarySearch<T>(IList<T>, T, IComparer<T>, out Int32)

Searches a sorted list for an item via binary search. The list must be sorted by the ordering in the passed instance of IComparer<T>.

Declaration
public static int BinarySearch<T>(IList<T> list, T item, IComparer<T> comparer, out int index)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The sorted list to search.

T item

The item to search for.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to sort the list. Only the Compare method is used.

System.Int32 index

Returns the first index at which the item can be found. If the return value is zero, indicating that item was not present in the list, then this returns the index at which item could be inserted to maintain the sorted order of the list.

Returns
Type Description
System.Int32

The number of items equal to item that appear in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

BinarySearch<T>(IList<T>, T, Comparison<T>, out Int32)

Searches a sorted list for an item via binary search. The list must be sorted by the ordering in the passed Comparison<T> delegate.

Declaration
public static int BinarySearch<T>(IList<T> list, T item, Comparison<T> comparison, out int index)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The sorted list to search.

T item

The item to search for.

System.Comparison<T> comparison

The comparison delegate used to sort the list.

System.Int32 index

Returns the first index at which the item can be found. If the return value is zero, indicating that item was not present in the list, then this returns the index at which item could be inserted to maintain the sorted order of the list.

Returns
Type Description
System.Int32

The number of items equal to item that appear in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

BinarySearch<T>(IList<T>, T, out Int32)

Searches a sorted list for an item via binary search. The list must be sorted by the natural ordering of the type (it's implementation of IComparable<T>).

Declaration
public static int BinarySearch<T>(IList<T> list, T item, out int index)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The sorted list to search.

T item

The item to search for.

System.Int32 index

Returns the first index at which the item can be found. If the return value is zero, indicating that item was not present in the list, then this returns the index at which item could be inserted to maintain the sorted order of the list.

Returns
Type Description
System.Int32

The number of items equal to item that appear in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

CartesianProduct<TFirst, TSecond>(IEnumerable<TFirst>, IEnumerable<TSecond>)

Computes the cartestian product of two collections: all possible pairs of items, with the first item taken from the first collection and the second item taken from the second collection. If the first collection has N items, and the second collection has M items, the cartesian product will have N * M pairs.

Declaration
public static IEnumerable<Pair<TFirst, TSecond>> CartesianProduct<TFirst, TSecond>(IEnumerable<TFirst> first, IEnumerable<TSecond> second)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<TFirst> first

The first collection.

System.Collections.Generic.IEnumerable<TSecond> second

The second collection.

Returns
Type Description
System.Collections.Generic.IEnumerable<Pair<TFirst, TSecond>>

An IEnumerable<Pair<TFirst, TSecond>> that enumerates the cartesian product of the two collections.

Type Parameters
Name Description
TFirst

The type of items in the first collection.

TSecond

The type of items in the second collection.

| Improve this Doc View Source

Concatenate<T>(IEnumerable<T>[])

Concatenates all the items from several collections. The collections need not be of the same type, but must have the same item type.

Declaration
public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] collections)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T>[] collections

The set of collections to concatenate. In many languages, this parameter can be specified as several individual parameters.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable that enumerates all the items in each of the collections, in order.

Type Parameters
Name Description
T
| Improve this Doc View Source

Convert<TSource, TDest>(IEnumerable<TSource>, Converter<TSource, TDest>)

Convert a collection of items by applying a delegate to each item in the collection. The resulting collection contains the result of applying converter to each item in sourceCollection, in order.

Declaration
public static IEnumerable<TDest> Convert<TSource, TDest>(IEnumerable<TSource> sourceCollection, Converter<TSource, TDest> converter)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<TSource> sourceCollection

The collection of item being converted.

System.Converter<TSource, TDest> converter

A delegate to the method to call, passing each item in sourceCollection.

Returns
Type Description
System.Collections.Generic.IEnumerable<TDest>

The resulting collection from applying converter to each item in sourceCollection, in order.

Type Parameters
Name Description
TSource

The type of items in the collection to convert.

TDest

The type each item is being converted to.

Exceptions
Type Condition
System.ArgumentNullException

sourceCollection or converter is null.

| Improve this Doc View Source

Copy<T>(IEnumerable<T>, T[], Int32)

Copies all of the items from the collection source to the array dest, starting at the index destIndex.

Declaration
public static void Copy<T>(IEnumerable<T> source, T[] dest, int destIndex)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> source

The collection that provide the source items.

T[] dest

The array to store the items into.

System.Int32 destIndex

The index to begin copying items to.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Length.

System.ArgumentNullException

source or dest is null.

System.ArgumentException

The collection has more items than will fit into the array. In this case, the array has been filled with as many items as fit before the exception is thrown.

| Improve this Doc View Source

Copy<T>(IEnumerable<T>, T[], Int32, Int32)

Copies at most count items from the collection source to the array dest, starting at the index destIndex. The source collection must not be the destination array or part thereof.

Declaration
public static void Copy<T>(IEnumerable<T> source, T[] dest, int destIndex, int count)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> source

The collection that provide the source items.

T[] dest

The array to store the items into.

System.Int32 destIndex

The index to begin copying items to.

System.Int32 count

The maximum number of items to copy. The array must be large enought to fit this number of items.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Length.

System.ArgumentOutOfRangeException

count is negative or destIndex + count is greater than dest.Length.

System.ArgumentNullException

source or dest is null.

| Improve this Doc View Source

Copy<T>(IEnumerable<T>, IList<T>, Int32)

Copies all of the items from the collection source to the list dest, starting at the index destIndex. If necessary, the size of the destination list is expanded.

Declaration
public static void Copy<T>(IEnumerable<T> source, IList<T> dest, int destIndex)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> source

The collection that provide the source items.

System.Collections.Generic.IList<T> dest

The list to store the items into.

System.Int32 destIndex

The index to begin copying items to.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Count.

System.ArgumentNullException

source or dest is null.

| Improve this Doc View Source

Copy<T>(IEnumerable<T>, IList<T>, Int32, Int32)

Copies at most count items from the collection source to the list dest, starting at the index destIndex. If necessary, the size of the destination list is expanded. The source collection must not be the destination list or part thereof.

Declaration
public static void Copy<T>(IEnumerable<T> source, IList<T> dest, int destIndex, int count)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> source

The collection that provide the source items.

System.Collections.Generic.IList<T> dest

The list to store the items into.

System.Int32 destIndex

The index to begin copying items to.

System.Int32 count

The maximum number of items to copy.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Count

System.ArgumentOutOfRangeException

count is negative.

System.ArgumentNullException

source or dest is null.

| Improve this Doc View Source

Copy<T>(IList<T>, Int32, T[], Int32, Int32)

Copies count items from the list or array source, starting at the index sourceIndex, to the array dest, starting at the index destIndex. The source may be the same as the destination array.

Declaration
public static void Copy<T>(IList<T> source, int sourceIndex, T[] dest, int destIndex, int count)
Parameters
Type Name Description
System.Collections.Generic.IList<T> source

The list or array that provide the source items.

System.Int32 sourceIndex

The index within sourceto begin copying items from.

T[] dest

The array to store the items into.

System.Int32 destIndex

The index within destto begin copying items to.

System.Int32 count

The maximum number of items to copy. The destination array must be large enough to hold this many items.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

sourceIndex is negative or greater than source.Count

System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Length

System.ArgumentOutOfRangeException

count is negative or too large.

System.ArgumentNullException

source or dest is null.

| Improve this Doc View Source

Copy<T>(IList<T>, Int32, IList<T>, Int32, Int32)

Copies count items from the list source, starting at the index sourceIndex, to the list dest, starting at the index destIndex. If necessary, the size of the destination list is expanded. The source and destination lists may be the same.

Declaration
public static void Copy<T>(IList<T> source, int sourceIndex, IList<T> dest, int destIndex, int count)
Parameters
Type Name Description
System.Collections.Generic.IList<T> source

The collection that provide the source items.

System.Int32 sourceIndex

The index within sourceto begin copying items from.

System.Collections.Generic.IList<T> dest

The list to store the items into.

System.Int32 destIndex

The index within destto begin copying items to.

System.Int32 count

The maximum number of items to copy.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

sourceIndex is negative or greater than source.Count

System.ArgumentOutOfRangeException

destIndex is negative or greater than dest.Count

System.ArgumentOutOfRangeException

count is negative or too large.

System.ArgumentNullException

source or dest is null.

| Improve this Doc View Source

Count<T>(IEnumerable<T>)

Count the number of items in an IEnumerable<T> collection. If a more specific collection type is being used, it is more efficient to use the Count property, if one is provided.

Declaration
public static int Count<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to count items in.

Returns
Type Description
System.Int32

The number of items in the collection.

Type Parameters
Name Description
T
Remarks

If the collection implements ICollection<T>, this method simply returns ICollection<T>.Count. Otherwise, it enumerates all items and counts them.

Exceptions
Type Condition
System.ArgumentNullException

collection is null.

| Improve this Doc View Source

CountEqual<T>(IEnumerable<T>, T)

Counts the number of items in the collection that are equal to find.

Declaration
public static int CountEqual<T>(IEnumerable<T> collection, T find)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to count items in.

T find

The item to compare to.

Returns
Type Description
System.Int32

The number of items in the collection that are equal to find.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

CountEqual<T>(IEnumerable<T>, T, IEqualityComparer<T>)

Counts the number of items in the collection that are equal to find.

Declaration
public static int CountEqual<T>(IEnumerable<T> collection, T find, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to count items in.

T find

The item to compare to.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The comparer to use to determine if two items are equal. Only the Equals member function will be called.

Returns
Type Description
System.Int32

The number of items in the collection that are equal to find.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentException

collection or equalityComparer is null.

| Improve this Doc View Source

CountWhere<T>(IEnumerable<T>, Predicate<T>)

Counts the number of items in the collection that satisfy the condition defined by predicate.

Declaration
public static int CountWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to count items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Int32

The number of items in the collection that satisfy predicate.

Type Parameters
Name Description
T
| Improve this Doc View Source

DisjointSets<T>(IEnumerable<T>, IEnumerable<T>)

Determines if two collections are disjoint, considered as sets. Two sets are disjoint if they have no common items.

Declaration
public static bool DisjointSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

Returns
Type Description
System.Boolean

True if collection1 are collection2 are disjoint, considered as sets.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsDisjoint method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

DisjointSets<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Determines if two collections are disjoint, considered as sets. Two sets are disjoint if they have no common items.

Declaration
public static bool DisjointSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparerComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Boolean

True if collection1 are collection2 are disjoint, considered as sets.

Type Parameters
Name Description
T
Remarks

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsDisjoint method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

EqualCollections<T>(IEnumerable<T>, IEnumerable<T>)

Determines if the two collections contain equal items in the same order. The two collections do not need to be of the same type; it is permissible to compare an array and an OrderedBag, for instance.

Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to compare.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to compare.

Returns
Type Description
System.Boolean

True if the collections have equal items in the same order. If both collections are empty, true is returned.

Type Parameters
Name Description
T

The type of items in the collections.

Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

EqualCollections<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Determines if the two collections contain equal items in the same order. The passed instance of IEqualityComparer<T> is used for determining if two items are equal.

Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to compare.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to compare.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals member function of this interface is called.

Returns
Type Description
System.Boolean

True if the collections have equal items in the same order. If both collections are empty, true is returned.

Type Parameters
Name Description
T

The type of items in the collections.

Exceptions
Type Condition
System.ArgumentNullException

collection1, collection2, or equalityComparer is null.

| Improve this Doc View Source

EqualCollections<T>(IEnumerable<T>, IEnumerable<T>, BinaryPredicate<T>)

Determines if the two collections contain "equal" items in the same order. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static bool EqualCollections<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to compare.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to compare.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality". This predicate can compute any relation between two items; it need not represent equality or an equivalence relation.

Returns
Type Description
System.Boolean

True if predicatereturns true for each corresponding pair of items in the two collections. If both collections are empty, true is returned.

Type Parameters
Name Description
T

The type of items in the collections.

Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be equality. For example, the following code determines if each integer in list1 is less than or equal to the corresponding integer in list2.

List<int> list1, list2;
if (EqualCollections(list1, list2, delegate(int x, int y) { return x <= y; }) {
    // the check is true...
}
Exceptions
Type Condition
System.ArgumentNullException

collection1, collection2, or predicate is null.

| Improve this Doc View Source

EqualSets<T>(IEnumerable<T>, IEnumerable<T>)

Determines if two collections are equal, considered as sets. Two sets are equal if they have have the same items, with order not being significant.

Declaration
public static bool EqualSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

Returns
Type Description
System.Boolean

True if collection1 are collection2 are equal, considered as sets.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the EqualTo method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

EqualSets<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Determines if two collections are equal, considered as sets. Two sets are equal if they have have the same items, with order not being significant.

Declaration
public static bool EqualSets<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Boolean

True if collection1 are collection2 are equal, considered as sets.

Type Parameters
Name Description
T
Remarks

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the EqualTo method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

Exists<T>(IEnumerable<T>, Predicate<T>)

Determines if a collection contains any item that satisfies the condition defined by predicate.

Declaration
public static bool Exists<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to check all the items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Boolean

True if the collection contains one or more items that satisfy the condition defined by predicate. False if the collection does not contain an item that satisfies predicate.

Type Parameters
Name Description
T
| Improve this Doc View Source

Fill<T>(T[], T)

Replaces each item in a array with a given value.

Declaration
public static void Fill<T>(T[] array, T value)
Parameters
Type Name Description
T[] array

The array to modify.

T value

The value to fill with.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentNullException

array is null.

| Improve this Doc View Source

Fill<T>(IList<T>, T)

Replaces each item in a list with a given value. The list does not change in size.

Declaration
public static void Fill<T>(IList<T> list, T value)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to modify.

T value

The value to fill with.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.ArgumentException

list is a read-only list.

System.ArgumentNullException

list is null.

| Improve this Doc View Source

FillRange<T>(T[], Int32, Int32, T)

Replaces each item in a part of a array with a given value.

Declaration
public static void FillRange<T>(T[] array, int start, int count, T value)
Parameters
Type Name Description
T[] array

The array to modify.

System.Int32 start

The index at which to start filling. The first index in the array has index 0.

System.Int32 count

The number of items to fill.

T value

The value to fill with.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentOutOfRangeException

start or count is negative, or start + count is greater than array.Length.

System.ArgumentNullException

array is null.

| Improve this Doc View Source

FillRange<T>(IList<T>, Int32, Int32, T)

Replaces each item in a part of a list with a given value.

Declaration
public static void FillRange<T>(IList<T> list, int start, int count, T value)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to modify.

System.Int32 start

The index at which to start filling. The first index in the list has index 0.

System.Int32 count

The number of items to fill.

T value

The value to fill with.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.ArgumentException

list is a read-only list.

System.ArgumentOutOfRangeException

start or count is negative, or start + count is greater than list.Count.

System.ArgumentNullException

list is null.

| Improve this Doc View Source

FindFirstIndexWhere<T>(IList<T>, Predicate<T>)

Finds the index of the first item in a list that satisfies the condition defined by predicate.

Declaration
public static int FindFirstIndexWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

Returns
Type Description
System.Int32

The index of the first item satisfying the condition. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

FindFirstWhere<T>(IEnumerable<T>, Predicate<T>)

Finds the first item in a collection that satisfies the condition defined by predicate.

Declaration
public static T FindFirstWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

Returns
Type Description
T

The first item in the collection that matches the condition, or the default value for T (0 or null) if no item that matches the condition is found.

Type Parameters
Name Description
T
Remarks

If the default value for T could be present in the collection, and would be matched by the predicate, then this method is inappropriate, because you cannot disguish whether the default value for T was actually present in the collection, or no items matched the predicate. In this case, use TryFindFirstWhere.

See Also
TryFindFirstWhere<T>(IEnumerable<T>, Predicate<T>, out T)
| Improve this Doc View Source

FindIndicesWhere<T>(IList<T>, Predicate<T>)

Enumerates the indices of all the items in list that satisfy the condition defined by predicate.

Declaration
public static IEnumerable<int> FindIndicesWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to check all the items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items that satisfy the condition.

Type Parameters
Name Description
T
| Improve this Doc View Source

FindLastIndexWhere<T>(IList<T>, Predicate<T>)

Finds the index of the last item in a list that satisfies the condition defined by predicate.

Declaration
public static int FindLastIndexWhere<T>(IList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

Returns
Type Description
System.Int32

The index of the last item satisfying the condition. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

FindLastWhere<T>(IEnumerable<T>, Predicate<T>)

Finds the last item in a collection that satisfies the condition defined by predicate.

Declaration
public static T FindLastWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

Returns
Type Description
T

The last item in the collection that matches the condition, or the default value for T (0 or null) if no item that matches the condition is found.

Type Parameters
Name Description
T
Remarks

If the collection implements IList<T>, then the list is scanned in reverse until a matching item is found. Otherwise, the entire collection is iterated in the forward direction.

If the default value for T could be present in the collection, and would be matched by the predicate, then this method is inappropriate, because you cannot disguish whether the default value for T was actually present in the collection, or no items matched the predicate. In this case, use TryFindFirstWhere.

See Also
TryFindLastWhere<T>(IEnumerable<T>, Predicate<T>, out T)
| Improve this Doc View Source

FindWhere<T>(IEnumerable<T>, Predicate<T>)

Enumerates all the items in collection that satisfy the condition defined by predicate.

Declaration
public static IEnumerable<T> FindWhere<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to check all the items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> that enumerates the items that satisfy the condition.

Type Parameters
Name Description
T
| Improve this Doc View Source

FirstConsecutiveEqual<T>(IList<T>, Int32)

Finds the first occurence of count consecutive equal items in the list.

Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to examine.

System.Int32 count

The number of consecutive equal items to look for. The count must be at least 1.

Returns
Type Description
System.Int32

The index of the first item in the first run of count consecutive equal items, or -1 if no such run exists..

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

FirstConsecutiveEqual<T>(IList<T>, Int32, IEqualityComparer<T>)

Finds the first occurence of count consecutive equal items in the list. A passed IEqualityComparer is used to determine equality.

Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to examine.

System.Int32 count

The number of consecutive equal items to look for. The count must be at least 1.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Int32

The index of the first item in the first run of count consecutive equal items, or -1 if no such run exists.

Type Parameters
Name Description
T
| Improve this Doc View Source

FirstConsecutiveEqual<T>(IList<T>, Int32, BinaryPredicate<T>)

Finds the first occurence of count consecutive "equal" items in the list. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static int FirstConsecutiveEqual<T>(IList<T> list, int count, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to examine.

System.Int32 count

The number of consecutive equal items to look for. The count must be at least 1.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Returns
Type Description
System.Int32

The index of the first item in the first run of count consecutive equal items, or -1 if no such run exists.

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be true equality.

| Improve this Doc View Source

FirstConsecutiveWhere<T>(IList<T>, Int32, Predicate<T>)

Finds the first occurence of count consecutive items in the list for which a given predicate returns true.

Declaration
public static int FirstConsecutiveWhere<T>(IList<T> list, int count, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to examine.

System.Int32 count

The number of consecutive items to look for. The count must be at least 1.

System.Predicate<T> predicate

The predicate used to test each item.

Returns
Type Description
System.Int32

The index of the first item in the first run of count items where predicate returns true for all items in the run, or -1 if no such run exists.

Type Parameters
Name Description
T
| Improve this Doc View Source

FirstIndexOf<T>(IList<T>, T)

Finds the index of the first item in a list equal to a given item.

Declaration
public static int FirstIndexOf<T>(IList<T> list, T item)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

Returns
Type Description
System.Int32

The index of the first item equal to item. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

FirstIndexOf<T>(IList<T>, T, IEqualityComparer<T>)

Finds the index of the first item in a list equal to a given item. A passed IEqualityComparer is used to determine equality.

Declaration
public static int FirstIndexOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Int32

The index of the first item equal to item. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

FirstIndexOfMany<T>(IList<T>, IEnumerable<T>)

Finds the index of the first item in a list equal to one of several given items.

Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

Returns
Type Description
System.Int32

The index of the first item equal to any of the items in the collection itemsToLookFor. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

FirstIndexOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)

Finds the index of the first item in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.

Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode methods will be called.

Returns
Type Description
System.Int32

The index of the first item equal to any of the items in the collection itemsToLookFor. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

FirstIndexOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)

Finds the index of the first item in a list "equal" to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static int FirstIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Returns
Type Description
System.Int32

The index of the first item "equal" to any of the items in the collection itemsToLookFor, using BinaryPredicate as the test for equality. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds first item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor

| Improve this Doc View Source

ForEach<T>(IEnumerable<T>, Action<T>)

Performs the specified action on each item in a collection.

Declaration
public static void ForEach<T>(IEnumerable<T> collection, Action<T> action)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

System.Action<T> action

An Action delegate which is invoked for each item in collection.

Type Parameters
Name Description
T
| Improve this Doc View Source

GeneratePermutations<T>(IEnumerable<T>)

Generates all the possible permutations of the items in collection. If collection has N items, then N factorial permutations will be generated. This method does not compare the items to determine if any of them are equal. If some items are equal, the same permutation may be generated more than once. For example, if the collections contains the three items A, A, and B, then this method will generate the six permutations, AAB, AAB, ABA, ABA, BAA, BAA (not necessarily in that order). To take equal items into account, use the GenerateSortedPermutations method.

Declaration
public static IEnumerable<T[]> GeneratePermutations<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to permute.

Returns
Type Description
System.Collections.Generic.IEnumerable<T[]>

An IEnumerable<T[]> that enumerations all the possible permutations of the items in collection. Each permutations is returned as an array. The items in the array should be copied if they need to be used after the next permutation is generated; each permutation may reuse the same array instance.

Type Parameters
Name Description
T

The type of items to permute.

| Improve this Doc View Source

GenerateSortedPermutations<T>(IEnumerable<T>)

Generates all the possible permutations of the items in collection, in lexicographical order. Even if some items are equal, the same permutation will not be generated more than once. For example, if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA, BAA.

Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to permute.

Returns
Type Description
System.Collections.Generic.IEnumerable<T[]>

An IEnumerable<T[]> that enumerations all the possible permutations of the items in collection. Each permutations is returned as an array. The items in the array should be copied if they need to be used after the next permutation is generated; each permutation may reuse the same array instance.

Type Parameters
Name Description
T

The type of items to permute.

| Improve this Doc View Source

GenerateSortedPermutations<T>(IEnumerable<T>, IComparer<T>)

Generates all the possible permutations of the items in collection, in lexicographical order. A supplied IComparer<T> instance is used to compare the items. Even if some items are equal, the same permutation will not be generated more than once. For example, if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA, BAA.

Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to permute.

System.Collections.Generic.IComparer<T> comparer

The IComparer<T> used to compare the items.

Returns
Type Description
System.Collections.Generic.IEnumerable<T[]>

An IEnumerable<T[]> that enumerations all the possible permutations of the items in collection. Each permutations is returned as an array. The items in the array should be copied if they need to be used after the next permutation is generated; each permutation may reuse the same array instance.

Type Parameters
Name Description
T

The type of items to permute.

| Improve this Doc View Source

GenerateSortedPermutations<T>(IEnumerable<T>, Comparison<T>)

Generates all the possible permutations of the items in collection, in lexicographical order. A supplied Comparison<T> delegate is used to compare the items. Even if some items are equal, the same permutation will not be generated more than once. For example, if the collections contains the three items A, A, and B, then this method will generate only the three permutations, AAB, ABA, BAA.

Declaration
public static IEnumerable<T[]> GenerateSortedPermutations<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to permute.

System.Comparison<T> comparison

The Comparison<T> delegate used to compare the items.

Returns
Type Description
System.Collections.Generic.IEnumerable<T[]>

An IEnumerable<T[]> that enumerations all the possible permutations of the items in collection. Each permutations is returned as an array. The items in the array should be copied if they need to be used after the next permutation is generated; each permutation may reuse the same array instance.

Type Parameters
Name Description
T

The type of items to permute.

| Improve this Doc View Source

GetCollectionEqualityComparer<T>()

Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, considered in order. This is the same notion of equality as in Algorithms.EqualCollections, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.

Declaration
public static IEqualityComparer<IEnumerable<T>> GetCollectionEqualityComparer<T>()
Returns
Type Description
System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>>

IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality.

Type Parameters
Name Description
T
Examples

The following code creates a Dictionary where the keys are a collection of strings.

    Dictionary<IEnumerable<string>, int> = 
        new Dictionary<IEnumerable<string>, int>(Algorithms.GetCollectionEqualityComparer<string>());
See Also
EqualCollections<T>(IEnumerable<T>, IEnumerable<T>)
| Improve this Doc View Source

GetCollectionEqualityComparer<T>(IEqualityComparer<T>)

Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, considered in order. This is the same notion of equality as in Algorithms.EqualCollections, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.

An IEqualityComparer<T> is used to determine if individual T's are equal

Declaration
public static IEqualityComparer<IEnumerable<T>> GetCollectionEqualityComparer<T>(IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEqualityComparer<T> equalityComparer

An IEqualityComparer<T> implementation used to compare individual T's.

Returns
Type Description
System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>>

IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality.

Type Parameters
Name Description
T
Examples

The following code creates a Dictionary where the keys are a collection of strings, compared in a case-insensitive way

    Dictionary<IEnumerable<string>, int> = 
        new Dictionary<IEnumerable<string>, int>(Algorithms.GetCollectionEqualityComparer<string>(StringComparer.CurrentCultureIgnoreCase));
See Also
EqualCollections<T>(IEnumerable<T>, IEnumerable<T>)
| Improve this Doc View Source

GetComparerFromComparison<T>(Comparison<T>)

Given a comparison delegate that compares two items of type T, gets an IComparer<T> instance that performs the same comparison.

Declaration
public static IComparer<T> GetComparerFromComparison<T>(Comparison<T> comparison)
Parameters
Type Name Description
System.Comparison<T> comparison

The comparison delegate to use.

Returns
Type Description
System.Collections.Generic.IComparer<T>

An IComparer<T> that performs the same comparing operation as comparison.

Type Parameters
Name Description
T
| Improve this Doc View Source

GetComparisonFromComparer<T>(IComparer<T>)

Given in IComparer<T> instenace that comparers two items from type T, gets a Comparison delegate that performs the same comparison.

Declaration
public static Comparison<T> GetComparisonFromComparer<T>(IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IComparer<T> comparer

The IComparer<T> instance to use.

Returns
Type Description
System.Comparison<T>

A Comparison<T> delegate that performans the same comparing operation as comparer.

Type Parameters
Name Description
T
| Improve this Doc View Source

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

Creates a delegate that converts keys to values by used a dictionary to map values. Keys that a not present in the dictionary are converted to the default value (zero or null).

Declaration
public static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
System.Collections.Generic.IDictionary<TKey, TValue> dictionary

The dictionary used to perform the conversion.

Returns
Type Description
System.Converter<TKey, TValue>

A delegate to a method that converts keys to values.

Type Parameters
Name Description
TKey
TValue
Remarks

This delegate can be used as a parameter in Convert or ConvertAll methods to convert entire collections.

| Improve this Doc View Source

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

Creates a delegate that converts keys to values by used a dictionary to map values. Keys that a not present in the dictionary are converted to a supplied default value.

Declaration
public static Converter<TKey, TValue> GetDictionaryConverter<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TValue defaultValue)
Parameters
Type Name Description
System.Collections.Generic.IDictionary<TKey, TValue> dictionary

The dictionary used to perform the conversion.

TValue defaultValue

The result of the conversion for keys that are not present in the dictionary.

Returns
Type Description
System.Converter<TKey, TValue>

A delegate to a method that converts keys to values.

Type Parameters
Name Description
TKey
TValue
Remarks

This delegate can be used as a parameter in Convert or ConvertAll methods to convert entire collections.

Exceptions
Type Condition
System.ArgumentNullException

dictionary is null.

| Improve this Doc View Source

GetIdentityComparer<T>()

Gets an IEqualityComparer<T> instance that can be used to compare objects of type T for object identity only. Two objects compare equal only if they are references to the same object.

Declaration
public static IEqualityComparer<T> GetIdentityComparer<T>()
    where T : class
Returns
Type Description
System.Collections.Generic.IEqualityComparer<T>

An IEqualityComparer<T> instance for identity comparison.

Type Parameters
Name Description
T
| Improve this Doc View Source

GetLexicographicalComparer<T>()

Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be used for collections or algorithms that use sequences of T as an item type. The Lexicographical ordered of sequences is for comparison.

Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>()
    where T : IComparable<T>
Returns
Type Description
System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>>

At IComparer<IEnumerable<T>> that compares sequences of T.

Type Parameters
Name Description
T
Remarks

T must implement either IComparable<T> and this implementation is used to compare the items.

| Improve this Doc View Source

GetLexicographicalComparer<T>(IComparer<T>)

Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be uses for collections or algorithms that use sequences of T as an item type. The Lexicographics ordered of sequences is for comparison.

Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>(IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IComparer<T> comparer

A comparer instance used to compare individual items of type T.

Returns
Type Description
System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>>

At IComparer<IEnumerable<T>> that compares sequences of T.

Type Parameters
Name Description
T
| Improve this Doc View Source

GetLexicographicalComparer<T>(Comparison<T>)

Creates an IComparer instance that can be used for comparing ordered sequences of type T; that is IEnumerable<Tgt;. This comparer can be uses for collections or algorithms that use sequences of T as an item type. The Lexicographics ordered of sequences is for comparison.

Declaration
public static IComparer<IEnumerable<T>> GetLexicographicalComparer<T>(Comparison<T> comparison)
Parameters
Type Name Description
System.Comparison<T> comparison

A comparison delegate used to compare individual items of type T.

Returns
Type Description
System.Collections.Generic.IComparer<System.Collections.Generic.IEnumerable<T>>

At IComparer<IEnumerable<T>> that compares sequences of T.

Type Parameters
Name Description
T
| Improve this Doc View Source

GetReverseComparer<T>(IComparer<T>)

Reverses the order of comparison of an IComparer<T>. The resulting comparer can be used, for example, to sort a collection in descending order. Equality and hash codes are unchanged.

Declaration
public static IComparer<T> GetReverseComparer<T>(IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IComparer<T> comparer

The comparer to reverse.

Returns
Type Description
System.Collections.Generic.IComparer<T>

An IComparer<T> that compares items in the reverse order of comparer.

Type Parameters
Name Description
T

The type of items thta are being compared.

Exceptions
Type Condition
System.ArgumentNullException

comparer is null.

| Improve this Doc View Source

GetReverseComparison<T>(Comparison<T>)

Reverses the order of comparison of an Comparison<T>. The resulting comparison can be used, for example, to sort a collection in descending order.

Declaration
public static Comparison<T> GetReverseComparison<T>(Comparison<T> comparison)
Parameters
Type Name Description
System.Comparison<T> comparison

The comparison to reverse.

Returns
Type Description
System.Comparison<T>

A Comparison<T> that compares items in the reverse order of comparison.

Type Parameters
Name Description
T

The type of items that are being compared.

Exceptions
Type Condition
System.ArgumentNullException

comparison is null.

| Improve this Doc View Source

GetSetEqualityComparer<T>()

Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, without regard to order. This is the same notion of equality as in Algorithms.EqualSets, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.

An IEqualityComparer<T> is used to determine if individual T's are equal

Declaration
public static IEqualityComparer<IEnumerable<T>> GetSetEqualityComparer<T>()
Returns
Type Description
System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>>

IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality, without regard to order.

Type Parameters
Name Description
T
Examples

The following code creates a Dictionary where the keys are a set of strings, without regard to order

    Dictionary<IEnumerable<string>, int> = 
        new Dictionary<IEnumerable<string>, int>(Algorithms.GetSetEqualityComparer<string>(StringComparer.CurrentCultureIgnoreCase));
See Also
EqualSets<T>(IEnumerable<T>, IEnumerable<T>)
| Improve this Doc View Source

GetSetEqualityComparer<T>(IEqualityComparer<T>)

Gets an IEqualityComparer<IEnumerable<T>> implementation that can be used to compare collections of elements (of type T). Two collections of T's are equal if they have the same number of items, and corresponding items are equal, without regard to order. This is the same notion of equality as in Algorithms.EqualSets, but encapsulated in an IEqualityComparer<IEnumerable<T>> implementation.

Declaration
public static IEqualityComparer<IEnumerable<T>> GetSetEqualityComparer<T>(IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEqualityComparer<T> equalityComparer

An IEqualityComparer<T> implementation used to compare individual T's.

Returns
Type Description
System.Collections.Generic.IEqualityComparer<System.Collections.Generic.IEnumerable<T>>

IEqualityComparer<IEnumerable<T>> implementation suitable for comparing collections of T for equality, without regard to order.

Type Parameters
Name Description
T
Examples

The following code creates a Dictionary where the keys are a set of strings, without regard to order

    Dictionary<IEnumerable<string>, int> = 
        new Dictionary<IEnumerable<string>, int>(Algorithms.GetSetEqualityComparer<string>());
See Also
EqualSets<T>(IEnumerable<T>, IEnumerable<T>)
| Improve this Doc View Source

IndexOfMaximum<T>(IList<T>)

Finds the index of the maximum value in a list.

Declaration
public static int IndexOfMaximum<T>(IList<T> list)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

Returns
Type Description
System.Int32

The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned.

Type Parameters
Name Description
T

The type of items in the list.

Remarks

Values in the list are compared by using the IComparable<T> interfaces implementation on the type T.

Exceptions
Type Condition
System.ArgumentNullException

list is null.

| Improve this Doc View Source

IndexOfMaximum<T>(IList<T>, IComparer<T>)

Finds the index of the maximum value in a list. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static int IndexOfMaximum<T>(IList<T> list, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection.

Returns
Type Description
System.Int32

The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.ArgumentNullException

list or comparer is null.

| Improve this Doc View Source

IndexOfMaximum<T>(IList<T>, Comparison<T>)

Finds the index of the maximum value in a list. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static int IndexOfMaximum<T>(IList<T> list, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Comparison<T> comparison

The comparison used to compare items in the collection.

Returns
Type Description
System.Int32

The index of the largest item in the list. If the maximum value appears multiple times, the index of the first appearance is used. If the list is empty, -1 is returned.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.ArgumentNullException

list or comparison is null.

| Improve this Doc View Source

IndexOfMinimum<T>(IList<T>)

Finds the index of the minimum value in a list.

Declaration
public static int IndexOfMinimum<T>(IList<T> list)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

Returns
Type Description
System.Int32

The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used.

Type Parameters
Name Description
T

The type of items in the list.

Remarks

Values in the list are compared by using the IComparable<T> interfaces implementation on the type T.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

list is null.

| Improve this Doc View Source

IndexOfMinimum<T>(IList<T>, IComparer<T>)

Finds the index of the minimum value in a list. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static int IndexOfMinimum<T>(IList<T> list, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection.

Returns
Type Description
System.Int32

The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

list or comparer is null.

| Improve this Doc View Source

IndexOfMinimum<T>(IList<T>, Comparison<T>)

Finds the index of the minimum value in a list. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static int IndexOfMinimum<T>(IList<T> list, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Comparison<T> comparison

The comparison delegate used to compare items in the collection.

Returns
Type Description
System.Int32

The index of the smallest item in the list. If the minimum value appears multiple times, the index of the first appearance is used.

Type Parameters
Name Description
T

The type of items in the list.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

list or comparison is null.

| Improve this Doc View Source

IndicesOf<T>(IList<T>, T)

Enumerates the indices of all the items in a list equal to a given item.

Declaration
public static IEnumerable<int> IndicesOf<T>(IList<T> list, T item)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items equal to item.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

IndicesOf<T>(IList<T>, T, IEqualityComparer<T>)

Enumerates the indices of all the items in a list equal to a given item. A passed IEqualityComparer is used to determine equality.

Declaration
public static IEnumerable<int> IndicesOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items equal to item.

Type Parameters
Name Description
T
| Improve this Doc View Source

IndicesOfMany<T>(IList<T>, IEnumerable<T>)

Enumerates the indices of all the items in a list equal to one of several given items.

Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

A collection of items to search for.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items equal to any of the items in the collection itemsToLookFor.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

IndicesOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)

Enumerates the indices of all the items in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.

Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

A collection of items to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality.

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items equal to any of the items in the collection itemsToLookFor.

Type Parameters
Name Description
T
| Improve this Doc View Source

IndicesOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)

Enumerates the indices of all the items in a list equal to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static IEnumerable<int> IndicesOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

A collection of items to search for.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>

An IEnumerable<T> that enumerates the indices of items "equal" to any of the items in the collection itemsToLookFor, using BinaryPredicate as the test for equality.

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds last item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor

| Improve this Doc View Source

IsProperSubsetOf<T>(IEnumerable<T>, IEnumerable<T>)

Determines if one collection is a proper subset of another, considered as sets. The first set is a proper subset of the second set if every item in the first set also occurs in the second set, and the first set is strictly smaller than the second set. If an item appears X times in the first set, it must appear at least X times in the second set.

Declaration
public static bool IsProperSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

Returns
Type Description
System.Boolean

True if collection1 is a subset of collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

IsProperSubsetOf<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Determines if one collection is a proper subset of another, considered as sets. The first set is a proper subset of the second set if every item in the first set also occurs in the second set, and the first set is strictly smaller than the second set. If an item appears X times in the first set, it must appear at least X times in the second set.

Declaration
public static bool IsProperSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Boolean

True if collection1 is a proper subset of collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

IsSubsetOf<T>(IEnumerable<T>, IEnumerable<T>)

Determines if one collection is a subset of another, considered as sets. The first set is a subset of the second set if every item in the first set also occurs in the second set. If an item appears X times in the first set, it must appear at least X times in the second set.

Declaration
public static bool IsSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

Returns
Type Description
System.Boolean

True if collection1 is a subset of collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

IsSubsetOf<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Determines if one collection is a subset of another, considered as sets. The first set is a subset of the second set if every item in the first set also occurs in the second set. If an item appears X times in the first set, it must appear at least X times in the second set.

Declaration
public static bool IsSubsetOf<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection.

System.Collections.Generic.IEnumerable<T> collection2

The second collection.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality.

Returns
Type Description
System.Boolean

True if collection1 is a subset of collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the IsSubsetOf method on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

LastIndexOf<T>(IList<T>, T)

Finds the index of the last item in a list equal to a given item.

Declaration
public static int LastIndexOf<T>(IList<T> list, T item)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

Returns
Type Description
System.Int32

The index of the last item equal to item. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

LastIndexOf<T>(IList<T>, T, IEqualityComparer<T>)

Finds the index of the last item in a list equal to a given item. A passed IEqualityComparer is used to determine equality.

Declaration
public static int LastIndexOf<T>(IList<T> list, T item, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

T item

The item to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Int32

The index of the last item equal to item. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

LastIndexOfMany<T>(IList<T>, IEnumerable<T>)

Finds the index of the last item in a list equal to one of several given items.

Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

Returns
Type Description
System.Int32

The index of the last item equal to any of the items in the collection itemsToLookFor. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

LastIndexOfMany<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)

Finds the index of the last item in a list equal to one of several given items. A passed IEqualityComparer is used to determine equality.

Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality.

Returns
Type Description
System.Int32

The index of the last item equal to any of the items in the collection itemsToLookFor. -1 if no such item exists in the list.

Type Parameters
Name Description
T
| Improve this Doc View Source

LastIndexOfMany<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)

Finds the index of the last item in a list "equal" to one of several given items. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static int LastIndexOfMany<T>(IList<T> list, IEnumerable<T> itemsToLookFor, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> itemsToLookFor

The items to search for.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Returns
Type Description
System.Int32

The index of the last item "equal" to any of the items in the collection itemsToLookFor, using BinaryPredicate as the test for equality. -1 if no such item exists in the list.

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality. This methods finds last item X which satisfies BinaryPredicate(X,Y), where Y is one of the items in itemsToLookFor

| Improve this Doc View Source

LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>)

Performs a lexicographical comparison of two sequences of values. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.

Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> sequence1

The first sequence to compare.

System.Collections.Generic.IEnumerable<T> sequence2

The second sequence to compare.

Returns
Type Description
System.Int32

Less than zero if sequence1 is lexicographically less than sequence2. Greater than zero if sequence1 is lexicographically greater than sequence2. Zero if sequence1 is equal to sequence2.

Type Parameters
Name Description
T

Types of items to compare. This type must implement IComparable<T> to allow items to be compared.

Remarks

T must implement either IComparable<T> and this implementation is used to compare the items.

Exceptions
Type Condition
System.NotSupportedException

T does not implement IComparable<T> or IComparable.

| Improve this Doc View Source

LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>, IComparer<T>)

Performs a lexicographical comparison of two sequences of values, using a supplied comparer interface. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.

Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> sequence1

The first sequence to compare.

System.Collections.Generic.IEnumerable<T> sequence2

The second sequence to compare.

System.Collections.Generic.IComparer<T> comparer

The IComparer<T> used to compare items. Only the Compare member function of this interface is called.

Returns
Type Description
System.Int32

Less than zero if sequence1 is lexicographically less than sequence2. Greater than zero if sequence1 is lexicographically greater than sequence2. Zero if sequence1 is equal to sequence2.

Type Parameters
Name Description
T

Types of items to compare.

Exceptions
Type Condition
System.ArgumentNullException

sequence1, sequence2, or comparer is null.

| Improve this Doc View Source

LexicographicalCompare<T>(IEnumerable<T>, IEnumerable<T>, Comparison<T>)

Performs a lexicographical comparison of two sequences of values, using a supplied comparison delegate. A lexicographical comparison compares corresponding pairs of elements from two sequences in order. If the first element of sequence1 is less than the first element of sequence2, then the comparison ends and the first sequence is lexicographically less than the second. If the first elements of each sequence are equal, then the comparison proceeds to the second element of each sequence. If one sequence is shorter than the other, but corresponding elements are all equal, then the shorter sequence is considered less than the longer one.

Declaration
public static int LexicographicalCompare<T>(IEnumerable<T> sequence1, IEnumerable<T> sequence2, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> sequence1

The first sequence to compare.

System.Collections.Generic.IEnumerable<T> sequence2

The second sequence to compare.

System.Comparison<T> comparison

The IComparison<T> delegate to compare items. Only the Compare member function of this interface is called.

Returns
Type Description
System.Int32

Less than zero if sequence1 is lexicographically less than sequence2. Greater than zero if sequence1 is lexicographically greater than sequence2. Zero if sequence1 is equal to sequence2.

Type Parameters
Name Description
T

Types of items to compare.

| Improve this Doc View Source

Maximum<T>(IEnumerable<T>)

Finds the maximum value in a collection.

Declaration
public static T Maximum<T>(IEnumerable<T> collection)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

Returns
Type Description
T

The largest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Remarks

Values in the collection are compared by using the IComparable<T> interfaces implementation on the type T.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection is null.

| Improve this Doc View Source

Maximum<T>(IEnumerable<T>, IComparer<T>)

Finds the maximum value in a collection. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static T Maximum<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection.

Returns
Type Description
T

The largest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection or comparer is null.

| Improve this Doc View Source

Maximum<T>(IEnumerable<T>, Comparison<T>)

Finds the maximum value in a collection. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static T Maximum<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Comparison<T> comparison

The comparison used to compare items in the collection.

Returns
Type Description
T

The largest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection or comparison is null.

| Improve this Doc View Source

MergeSorted<T>(IComparer<T>, IEnumerable<T>[])

Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the ordering in the passed instance of IComparer<T>. The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.

Declaration
public static IEnumerable<T> MergeSorted<T>(IComparer<T> comparer, params IEnumerable<T>[] collections)
Parameters
Type Name Description
System.Collections.Generic.IComparer<T> comparer

The comparer instance used to sort the list. Only the Compare method is used.

System.Collections.Generic.IEnumerable<T>[] collections

The set of collections to merge. In many languages, this parameter can be specified as several individual parameters.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> that enumerates all the items in all the collections in sorted order.

Type Parameters
Name Description
T
| Improve this Doc View Source

MergeSorted<T>(IEnumerable<T>[])

Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the natural ordering of the type (it's implementation of IComparable<T>). The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.

Declaration
public static IEnumerable<T> MergeSorted<T>(params IEnumerable<T>[] collections)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T>[] collections

The set of collections to merge. In many languages, this parameter can be specified as several individual parameters.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> that enumerates all the items in all the collections in sorted order.

Type Parameters
Name Description
T
| Improve this Doc View Source

MergeSorted<T>(Comparison<T>, IEnumerable<T>[])

Merge several sorted collections into a single sorted collection. Each input collection must be sorted by the ordering in the passed Comparison<T> delegate. The merging is stable; equal items maintain their ordering, and equal items in different collections are placed in the order of the collections.

Declaration
public static IEnumerable<T> MergeSorted<T>(Comparison<T> comparison, params IEnumerable<T>[] collections)
Parameters
Type Name Description
System.Comparison<T> comparison

The comparison delegate used to sort the collections.

System.Collections.Generic.IEnumerable<T>[] collections

The set of collections to merge. In many languages, this parameter can be specified as several individual parameters.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> that enumerates all the items in all the collections in sorted order.

Type Parameters
Name Description
T
| Improve this Doc View Source

Minimum<T>(IEnumerable<T>)

Finds the minimum value in a collection.

Declaration
public static T Minimum<T>(IEnumerable<T> collection)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

Returns
Type Description
T

The smallest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Remarks

Values in the collection are compared by using the IComparable<T> interfaces implementation on the type T.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection is null.

| Improve this Doc View Source

Minimum<T>(IEnumerable<T>, IComparer<T>)

Finds the minimum value in a collection. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static T Minimum<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection.

Returns
Type Description
T

The smallest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection or comparer is null.

| Improve this Doc View Source

Minimum<T>(IEnumerable<T>, Comparison<T>)

Finds the minimum value in a collection. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static T Minimum<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Comparison<T> comparison

The comparison used to compare items in the collection.

Returns
Type Description
T

The smallest item in the collection.

Type Parameters
Name Description
T

The type of items in the collection.

Exceptions
Type Condition
System.InvalidOperationException

The collection is empty.

System.ArgumentNullException

collection or comparison is null.

| Improve this Doc View Source

NCopiesOf<T>(Int32, T)

Creates an IEnumerator that enumerates a given item n times.

Declaration
public static IEnumerable<T> NCopiesOf<T>(int n, T item)
Parameters
Type Name Description
System.Int32 n

The number of times to enumerate the item.

T item

The item that should occur in the enumeration.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An IEnumerable<T> that yields n copies of item.

Type Parameters
Name Description
T
Examples

The following creates a list consisting of 1000 copies of the double 1.0.

List<double> list = new List<double>(Algorithms.NCopiesOf(1000, 1.0));
Exceptions
Type Condition
System.ArgumentOutOfRangeException

The argument n is less than zero.

| Improve this Doc View Source

Partition<T>(IList<T>, Predicate<T>)

Partition a list or array based on a predicate. After partitioning, all items for which the predicate returned true precede all items for which the predicate returned false.

Declaration
public static int Partition<T>(IList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to partition.

System.Predicate<T> predicate

A delegate that defines the partitioning condition.

Returns
Type Description
System.Int32

The index of the first item in the second half of the partition; i.e., the first item for which predicate returned false. If the predicate was true for all items in the list, list.Count is returned.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RandomShuffle<T>(IEnumerable<T>)

Randomly shuffles the items in a collection, yielding a new collection.

Declaration
public static T[] RandomShuffle<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to shuffle.

Returns
Type Description
T[]

An array with the same size and items as collection, but the items in a randomly chosen order.

Type Parameters
Name Description
T

The type of the items in the collection.

| Improve this Doc View Source

RandomShuffle<T>(IEnumerable<T>, Random)

Randomly shuffles the items in a collection, yielding a new collection.

Declaration
public static T[] RandomShuffle<T>(IEnumerable<T> collection, Random randomGenerator)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to shuffle.

System.Random randomGenerator

The random number generator to use to select the random order.

Returns
Type Description
T[]

An array with the same size and items as collection, but the items in a randomly chosen order.

Type Parameters
Name Description
T

The type of the items in the collection.

| Improve this Doc View Source

RandomShuffleInPlace<T>(IList<T>)

Randomly shuffles the items in a list or array, in place.

Declaration
public static void RandomShuffleInPlace<T>(IList<T> list)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to shuffle.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RandomShuffleInPlace<T>(IList<T>, Random)

Randomly shuffles the items in a list or array, in place.

Declaration
public static void RandomShuffleInPlace<T>(IList<T> list, Random randomGenerator)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to shuffle.

System.Random randomGenerator

The random number generator to use to select the random order.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RandomSubset<T>(IEnumerable<T>, Int32)

Picks a random subset of count items from collection, and places those items into a random order. No item is selected more than once.

Declaration
public static T[] RandomSubset<T>(IEnumerable<T> collection, int count)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to select from. This collection is not changed.

System.Int32 count

The number of items in the subset to choose.

Returns
Type Description
T[]

An array of count items, selected at random from collection.

Type Parameters
Name Description
T

The type of items in the collection.

Remarks

If the collection implements IList<T>, then this method takes time O(count). Otherwise, this method takes time O(N), where N is the number of items in the collection.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

count is negative or greater than collection.Count.

| Improve this Doc View Source

RandomSubset<T>(IEnumerable<T>, Int32, Random)

Picks a random subset of count items from collection, and places those items into a random order. No item is selected more than once.

Declaration
public static T[] RandomSubset<T>(IEnumerable<T> collection, int count, Random randomGenerator)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection of items to select from. This collection is not changed.

System.Int32 count

The number of items in the subset to choose.

System.Random randomGenerator

The random number generates used to make the selection.

Returns
Type Description
T[]

An array of count items, selected at random from collection.

Type Parameters
Name Description
T

The type of items in the collection.

Remarks

If the collection implements IList<T>, then this method takes time O(count). Otherwise, this method takes time O(N), where N is the number of items in the collection.

Exceptions
Type Condition
System.ArgumentOutOfRangeException

count is negative or greater than list.Count.

System.ArgumentNullException

randomGenerator is null.

| Improve this Doc View Source

Range<T>(T[], Int32, Int32)

Returns a view onto a sub-range of an array. Items from array are not copied; the returned IList<T> is simply a different view onto the same underlying items. Changes to array are reflected in the view, and vice versa. Insertions and deletions in the view change the size of the view. After an insertion, the last item in array "falls off the end". After a deletion, the last item in array becomes the default value (0 or null).

Declaration
public static IList<T> Range<T>(T[] array, int start, int count)
Parameters
Type Name Description
T[] array

The array to view.

System.Int32 start

The starting index of the view.

System.Int32 count

The number of items in the view.

Returns
Type Description
System.Collections.Generic.IList<T>

A list that is a view onto the given sub-array.

Type Parameters
Name Description
T
Remarks

This method can be used to apply an algorithm to a portion of a array. For example:

Algorithms.ReverseInPlace(Algorithms.Range(array, 3, 6))

will reverse the 6 items beginning at index 3.

Exceptions
Type Condition
System.ArgumentNullException

array is null.

System.ArgumentOutOfRangeException

start or count is negative.

System.ArgumentOutOfRangeException

start + count is greater than the size of array.

| Improve this Doc View Source

Range<T>(IList<T>, Int32, Int32)

Returns a view onto a sub-range of a list. Items from list are not copied; the returned IList<T> is simply a different view onto the same underlying items. Changes to list are reflected in the view, and vice versa. Insertions and deletions in the view change the size of the view, but insertions and deletions in the underlying list do not.

Declaration
public static IList<T> Range<T>(IList<T> list, int start, int count)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to view.

System.Int32 start

The starting index of the view.

System.Int32 count

The number of items in the view.

Returns
Type Description
System.Collections.Generic.IList<T>

A list that is a view onto the given sub-list.

Type Parameters
Name Description
T

The type of the items in the list.

Remarks

This method can be used to apply an algorithm to a portion of a list. For example:

Algorithms.ReverseInPlace(Algorithms.Range(list, 3, 6))

will reverse the 6 items beginning at index 3.

Exceptions
Type Condition
System.ArgumentNullException

list is null.

System.ArgumentOutOfRangeException

start or count is negative.

System.ArgumentOutOfRangeException

start + count is greater than the size of list.

| Improve this Doc View Source

ReadOnly<T>(ICollection<T>)

Returns a read-only view onto a collection. The returned ICollection<T> interface only allows operations that do not change the collection: GetEnumerator, Contains, CopyTo, Count. The ReadOnly property returns false, indicating that the collection is read-only. All other methods on the interface throw a NotSupportedException.

Declaration
public static ICollection<T> ReadOnly<T>(ICollection<T> collection)
Parameters
Type Name Description
System.Collections.Generic.ICollection<T> collection

The collection to wrap.

Returns
Type Description
System.Collections.Generic.ICollection<T>

A read-only view onto collection. If collection is null, then null is returned.

Type Parameters
Name Description
T

The type of items in the collection.

Remarks

The data in the underlying collection is not copied. If the underlying collection is changed, then the read-only view also changes accordingly.

| Improve this Doc View Source

ReadOnly<T>(IList<T>)

Returns a read-only view onto a list. The returned IList<T> interface only allows operations that do not change the list: GetEnumerator, Contains, CopyTo, Count, IndexOf, and the get accessor of the indexer. The IsReadOnly property returns true, indicating that the list is read-only. All other methods on the interface throw a NotSupportedException.

Declaration
public static IList<T> ReadOnly<T>(IList<T> list)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to wrap.

Returns
Type Description
System.Collections.Generic.IList<T>

A read-only view onto list. Returns null if list is null. If list is already read-only, returns list.

Type Parameters
Name Description
T

The type of items in the list.

Remarks

The data in the underlying list is not copied. If the underlying list is changed, then the read-only view also changes accordingly.

| Improve this Doc View Source

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

Returns a read-only view onto a dictionary. The returned IDictionary<TKey,TValue> interface only allows operations that do not change the dictionary. The IsReadOnly property returns true, indicating that the dictionary is read-only. All other methods on the interface throw a NotSupportedException.

Declaration
public static IDictionary<TKey, TValue> ReadOnly<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
System.Collections.Generic.IDictionary<TKey, TValue> dictionary

The dictionary to wrap.

Returns
Type Description
System.Collections.Generic.IDictionary<TKey, TValue>

A read-only view onto dictionary. Returns null if dictionary is null. If dictionary is already read-only, returns dictionary.

Type Parameters
Name Description
TKey
TValue
Remarks

The data in the underlying dictionary is not copied. If the underlying dictionary is changed, then the read-only view also changes accordingly.

| Improve this Doc View Source

ReadWriteList<T>(T[])

Creates a read-write IList<T> wrapper around an array. When an array is implicitely converted to an IList<T>, changes to the items in the array cannot be made through the interface. This method creates a read-write IList<T> wrapper on an array that can be used to make changes to the array.

Use this method when you need to pass an array to an algorithms that takes an IList<T> and that tries to modify items in the list. Algorithms in this class generally do not need this method, since they have been design to operate on arrays even when they are passed as an IList<T>.

Declaration
public static IList<T> ReadWriteList<T>(T[] array)
Parameters
Type Name Description
T[] array

The array to wrap.

Returns
Type Description
System.Collections.Generic.IList<T>

An IList<T> wrapper onto array.

Type Parameters
Name Description
T
Remarks

Since arrays cannot be resized, inserting an item causes the last item in the array to be automatically removed. Removing an item causes the last item in the array to be replaced with a default value (0 or null). Clearing the list causes all the items to be replaced with a default value.

| Improve this Doc View Source

RemoveDuplicates<T>(IEnumerable<T>)

Remove consecutive equal items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed.

Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with consecutive duplicates removed.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

Exceptions
Type Condition
System.ArgumentNullException

collection is null.

| Improve this Doc View Source

RemoveDuplicates<T>(IEnumerable<T>, IEqualityComparer<T>)

Remove consecutive equal items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed. A passed IEqualityComparer is used to determine equality.

Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with consecutive duplicates removed.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentNullException

collection or equalityComparer is null.

| Improve this Doc View Source

RemoveDuplicates<T>(IEnumerable<T>, BinaryPredicate<T>)

Remove consecutive "equal" items from a collection, yielding another collection. In each run of consecutive equal items in the collection, all items after the first item in the run are removed. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static IEnumerable<T> RemoveDuplicates<T>(IEnumerable<T> collection, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality". An item current is removed if predicate(first, current)==true, where first is the first item in the group of "duplicate" items.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with consecutive "duplicates" removed.

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being removed need not be true equality.

| Improve this Doc View Source

RemoveDuplicatesInPlace<T>(IList<T>)

Remove consecutive equal items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The removal is done in-place, changing the list.

Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RemoveDuplicatesInPlace<T>(IList<T>, IEqualityComparer<T>)

Remove subsequent consecutive equal items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The replacement is done in-place, changing the list. A passed IEqualityComparer is used to determine equality.

Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RemoveDuplicatesInPlace<T>(IList<T>, BinaryPredicate<T>)

Remove consecutive "equal" items from a list or array. In each run of consecutive equal items in the list, all items after the first item in the run are removed. The replacement is done in-place, changing the list. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static void RemoveDuplicatesInPlace<T>(IList<T> list, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Type Parameters
Name Description
T
Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being tested for need not be true equality.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

RemoveWhere<T>(ICollection<T>, Predicate<T>)

Removes all the items in the collection that satisfy the condition defined by predicate.

Declaration
public static ICollection<T> RemoveWhere<T>(ICollection<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.ICollection<T> collection

The collection to check all the items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Collections.Generic.ICollection<T>

Returns a collection of the items that were removed. This collection contains the items in the same order that they orginally appeared in collection.

Type Parameters
Name Description
T
Remarks

If the collection if an array or implements IList<T>, an efficient algorithm that compacts items is used. If not, then ICollection<T>.Remove is used to remove items from the collection. If the collection is an array or fixed-size list, the non-removed elements are placed, in order, at the beginning of the list, and the remaining list items are filled with a default value (0 or null).

| Improve this Doc View Source

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

Replace all items in a collection equal to a particular value with another values, yielding another collection.

Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, T itemFind, T replaceWith)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

T itemFind

The value to find and replace within collection.

T replaceWith

The new value to replace with.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with the appropriate replacements made.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

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

Replace all items in a collection equal to a particular value with another values, yielding another collection. A passed IEqualityComparer is used to determine equality.

Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, T itemFind, T replaceWith, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

T itemFind

The value to find and replace within collection.

T replaceWith

The new value to replace with.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with the appropriate replacements made.

Type Parameters
Name Description
T
| Improve this Doc View Source

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

Replace all items in a collection that a predicate evalues at true with a value, yielding another collection. .

Declaration
public static IEnumerable<T> Replace<T>(IEnumerable<T> collection, Predicate<T> predicate, T replaceWith)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to process.

System.Predicate<T> predicate

The predicate used to evaluate items with the collection. If the predicate returns true for a particular item, the item is replaces with replaceWith.

T replaceWith

The new value to replace with.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

An new collection with the items from collection, in the same order, with the appropriate replacements made.

Type Parameters
Name Description
T
| Improve this Doc View Source

ReplaceInPlace<T>(IList<T>, T, T)

Replace all items in a list or array equal to a particular value with another value. The replacement is done in-place, changing the list.

Declaration
public static void ReplaceInPlace<T>(IList<T> list, T itemFind, T replaceWith)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

T itemFind

The value to find and replace within collection.

T replaceWith

The new value to replace with.

Type Parameters
Name Description
T
Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

ReplaceInPlace<T>(IList<T>, T, T, IEqualityComparer<T>)

Replace all items in a list or array equal to a particular value with another values. The replacement is done in-place, changing the list. A passed IEqualityComparer is used to determine equality.

Declaration
public static void ReplaceInPlace<T>(IList<T> list, T itemFind, T replaceWith, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

T itemFind

The value to find and replace within collection.

T replaceWith

The new value to replace with.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

ReplaceInPlace<T>(IList<T>, Predicate<T>, T)

Replace all items in a list or array that a predicate evaluates at true with a value. The replacement is done in-place, changing the list.

Declaration
public static void ReplaceInPlace<T>(IList<T> list, Predicate<T> predicate, T replaceWith)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to process.

System.Predicate<T> predicate

The predicate used to evaluate items with the collection. If the predicate returns true for a particular item, the item is replaces with replaceWith.

T replaceWith

The new value to replace with.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

Reverse<T>(IList<T>)

Reverses a list and returns the reversed list, without changing the source list.

Declaration
public static IEnumerable<T> Reverse<T>(IList<T> source)
Parameters
Type Name Description
System.Collections.Generic.IList<T> source

The list to reverse.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

A collection that contains the items from source in reverse order.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentNullException

source is null.

| Improve this Doc View Source

ReverseInPlace<T>(IList<T>)

Reverses a list or array in place.

Declaration
public static void ReverseInPlace<T>(IList<T> list)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to reverse.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

Exceptions
Type Condition
System.ArgumentNullException

list is null.

System.ArgumentException

list is read only.

| Improve this Doc View Source

Rotate<T>(IList<T>, Int32)

Rotates a list and returns the rotated list, without changing the source list.

Declaration
public static IEnumerable<T> Rotate<T>(IList<T> source, int amountToRotate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> source

The list to rotate.

System.Int32 amountToRotate

The number of elements to rotate. This value can be positive or negative. For example, rotating by positive 3 means that source[3] is the first item in the returned collection. Rotating by negative 3 means that source[source.Count - 3] is the first item in the returned collection.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

A collection that contains the items from source in rotated order.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentNullException

source is null.

| Improve this Doc View Source

RotateInPlace<T>(IList<T>, Int32)

Rotates a list or array in place.

Declaration
public static void RotateInPlace<T>(IList<T> list, int amountToRotate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to rotate.

System.Int32 amountToRotate

The number of elements to rotate. This value can be positive or negative. For example, rotating by positive 3 means that list[3] is the first item in the resulting list. Rotating by negative 3 means that list[list.Count - 3] is the first item in the resulting list.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

Exceptions
Type Condition
System.ArgumentNullException

list is null.

| Improve this Doc View Source

SearchForSubsequence<T>(IList<T>, IEnumerable<T>)

Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence of list matches pattern at index i if list[i] is equal to the first item in pattern, list[i+1] is equal to the second item in pattern, and so forth for all the items in pattern.

Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> pattern

The sequence of items to search for.

Returns
Type Description
System.Int32

The first index with list that matches the items in pattern.

Type Parameters
Name Description
T

The type of items in the list.

Remarks

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

| Improve this Doc View Source

SearchForSubsequence<T>(IList<T>, IEnumerable<T>, IEqualityComparer<T>)

Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence of list matches pattern at index i if list[i] is equal to the first item in pattern, list[i+1] is equal to the second item in pattern, and so forth for all the items in pattern. The passed instance of IEqualityComparer<T> is used for determining if two items are equal.

Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> pattern

The sequence of items to search for.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals method will be called.

Returns
Type Description
System.Int32

The first index with list that matches the items in pattern.

Type Parameters
Name Description
T

The type of items in the list.

| Improve this Doc View Source

SearchForSubsequence<T>(IList<T>, IEnumerable<T>, BinaryPredicate<T>)

Searchs a list for a sub-sequence of items that match a particular pattern. A subsequence of list matches pattern at index i if list[i] is "equal" to the first item in pattern, list[i+1] is "equal" to the second item in pattern, and so forth for all the items in pattern. The passed BinaryPredicate is used to determine if two items are "equal".

Declaration
public static int SearchForSubsequence<T>(IList<T> list, IEnumerable<T> pattern, BinaryPredicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list to search.

System.Collections.Generic.IEnumerable<T> pattern

The sequence of items to search for.

BinaryPredicate<T> predicate

The BinaryPredicate used to compare items for "equality".

Returns
Type Description
System.Int32

The first index with list that matches the items in pattern.

Type Parameters
Name Description
T

The type of items in the list.

Remarks

Since an arbitrary BinaryPredicate is passed to this function, what is being tested for in the pattern need not be equality.

| Improve this Doc View Source

SetDifference<T>(IEnumerable<T>, IEnumerable<T>)

Computes the set-theoretic difference of two collections. The difference of two sets is all items that appear in the first set, but not in the second. If an item appears X times in the first set, and Y times in the second set, the difference contains the item X - Y times (0 times if X < Y). The source collections are not changed. A new collection is created with the difference of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to difference.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to difference.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The difference of collection1 and collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Difference or DifferenceWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetDifference<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Computes the set-theoretic difference of two collections. The difference of two sets is all items that appear in the first set, but not in the second. If an item appears X times in the first set, and Y times in the second set, the difference contains the item X - Y times (0 times if X < Y). The source collections are not changed. A new collection is created with the difference of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to difference.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to difference.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The difference of collection1 and collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the difference or differenceWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetIntersection<T>(IEnumerable<T>, IEnumerable<T>)

Computes the set-theoretic intersection of two collections. The intersection of two sets is all items that appear in both of the sets. If an item appears X times in one set, and Y times in the other set, the intersection contains the item Minimum(X,Y) times. The source collections are not changed. A new collection is created with the intersection of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetIntersection<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to intersect.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to intersect.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The intersection of the two collections, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Intersection or IntersectionWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetIntersection<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Computes the set-theoretic intersection of two collections. The intersection of two sets is all items that appear in both of the sets. If an item appears X times in one set, and Y times in the other set, the intersection contains the item Minimum(X,Y) times. The source collections are not changed. A new collection is created with the intersection of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetIntersection<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to intersect.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to intersect.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The intersection of the two collections, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Intersection or IntersectionWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetSymmetricDifference<T>(IEnumerable<T>, IEnumerable<T>)

Computes the set-theoretic symmetric difference of two collections. The symmetric difference of two sets is all items that appear in the one of the sets, but not in the other. If an item appears X times in the one set, and Y times in the other set, the symmetric difference contains the item AbsoluteValue(X - Y) times. The source collections are not changed. A new collection is created with the symmetric difference of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetSymmetricDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to symmetric difference.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to symmetric difference.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The symmetric difference of collection1 and collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the SymmetricDifference or SymmetricDifferenceWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetSymmetricDifference<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Computes the set-theoretic symmetric difference of two collections. The symmetric difference of two sets is all items that appear in the one of the sets, but not in the other. If an item appears X times in the one set, and Y times in the other set, the symmetric difference contains the item AbsoluteValue(X - Y) times. The source collections are not changed. A new collection is created with the symmetric difference of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetSymmetricDifference<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to symmetric difference.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to symmetric difference.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The symmetric difference of collection1 and collection2, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the symmetric difference or symmetric differenceWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetUnion<T>(IEnumerable<T>, IEnumerable<T>)

Computes the set-theoretic union of two collections. The union of two sets is all items that appear in either of the sets. If an item appears X times in one set, and Y times in the other set, the union contains the item Maximum(X,Y) times. The source collections are not changed. A new collection is created with the union of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetUnion<T>(IEnumerable<T> collection1, IEnumerable<T> collection2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to union.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to union.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The union of the two collections, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

The default sense of equality for T is used, as defined by T's implementation of IComparable<T>.Equals or object.Equals.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the Union or UnionWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

SetUnion<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)

Computes the set-theoretic union of two collections. The union of two sets is all items that appear in either of the sets. If an item appears X times in one set, and Y times in the other set, the union contains the item Maximum(X,Y) times. The source collections are not changed. A new collection is created with the union of the collections; the order of the items in this collection is undefined.

Declaration
public static IEnumerable<T> SetUnion<T>(IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> equalityComparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection1

The first collection to union.

System.Collections.Generic.IEnumerable<T> collection2

The second collection to union.

System.Collections.Generic.IEqualityComparer<T> equalityComparer

The IEqualityComparer<T> used to compare items for equality. Only the Equals and GetHashCode member functions of this interface are called.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

The union of the two collections, considered as sets.

Type Parameters
Name Description
T
Remarks

When equal items appear in both collections, the returned collection will include an arbitrary choice of one of the two equal items.

If both collections are Set, Bag, OrderedSet, or OrderedBag collections, it is more efficient to use the union or unionWith methods on that class.

Exceptions
Type Condition
System.ArgumentNullException

collection1 or collection2 is null.

| Improve this Doc View Source

Sort<T>(IEnumerable<T>)

Creates a sorted version of a collection.

Declaration
public static T[] Sort<T>(IEnumerable<T> collection)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
Remarks

Values are compared by using the IComparable<T> interfaces implementation on the type T.

| Improve this Doc View Source

Sort<T>(IEnumerable<T>, IComparer<T>)

Creates a sorted version of a collection. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static T[] Sort<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection. Only the Compare method is used.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
| Improve this Doc View Source

Sort<T>(IEnumerable<T>, Comparison<T>)

Creates a sorted version of a collection. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static T[] Sort<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

System.Comparison<T> comparison

The comparison delegate used to compare items in the collection.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
| Improve this Doc View Source

SortInPlace<T>(IList<T>)

Sorts a list or array in place.

Declaration
public static void SortInPlace<T>(IList<T> list)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

Type Parameters
Name Description
T
Remarks

The Quicksort algorithms is used to sort the items. In virtually all cases, this takes time O(N log N), where N is the number of items in the list.

Values are compared by using the IComparable<T> interfaces implementation on the type T.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

SortInPlace<T>(IList<T>, IComparer<T>)

Sorts a list or array in place. A supplied IComparer<T> is used to compare the items in the list.

Declaration
public static void SortInPlace<T>(IList<T> list, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection. Only the Compare method is used.

Type Parameters
Name Description
T
Remarks

The Quicksort algorithms is used to sort the items. In virtually all cases, this takes time O(N log N), where N is the number of items in the list.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

SortInPlace<T>(IList<T>, Comparison<T>)

Sorts a list or array in place. A supplied Comparison<T> delegate is used to compare the items in the list.

Declaration
public static void SortInPlace<T>(IList<T> list, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

System.Comparison<T> comparison

The comparison delegate used to compare items in the collection.

Type Parameters
Name Description
T
Remarks

The Quicksort algorithms is used to sort the items. In virtually all cases, this takes time O(N log N), where N is the number of items in the list.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

StablePartition<T>(IList<T>, Predicate<T>)

Partition a list or array based on a predicate. After partitioning, all items for which the predicate returned true precede all items for which the predicate returned false. The partition is stable, which means that if items X and Y have the same result from the predicate, and X precedes Y in the original list, X will precede Y in the partitioned list.

Declaration
public static int StablePartition<T>(IList<T> list, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to partition.

System.Predicate<T> predicate

A delegate that defines the partitioning condition.

Returns
Type Description
System.Int32

The index of the first item in the second half of the partition; i.e., the first item for which predicate returned false. If the predicate was true for all items in the list, list.Count is returned.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

StableSort<T>(IEnumerable<T>)

Creates a sorted version of a collection. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection.

Declaration
public static T[] StableSort<T>(IEnumerable<T> collection)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
Remarks

Values are compared by using the IComparable<T> interfaces implementation on the type T.

| Improve this Doc View Source

StableSort<T>(IEnumerable<T>, IComparer<T>)

Creates a sorted version of a collection. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection. A supplied IComparer<T> is used to compare the items in the collection.

Declaration
public static T[] StableSort<T>(IEnumerable<T> collection, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection. Only the Compare method is used.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
| Improve this Doc View Source

StableSort<T>(IEnumerable<T>, Comparison<T>)

Creates a sorted version of a collection. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection. A supplied Comparison<T> delegate is used to compare the items in the collection.

Declaration
public static T[] StableSort<T>(IEnumerable<T> collection, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to sort.

System.Comparison<T> comparison

The comparison delegate used to compare items in the collection.

Returns
Type Description
T[]

An array containing the sorted version of the collection.

Type Parameters
Name Description
T
Remarks

Values are compared by using the IComparable<T> interfaces implementation on the type T.

| Improve this Doc View Source

StableSortInPlace<T>(IList<T>)

Sorts a list or array in place. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection.

Declaration
public static void StableSortInPlace<T>(IList<T> list)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

Type Parameters
Name Description
T
Remarks

Values are compared by using the IComparable<T> interfaces implementation on the type T.

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

StableSortInPlace<T>(IList<T>, IComparer<T>)

Sorts a list or array in place. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection. A supplied IComparer<T> is used to compare the items in the list.

Declaration
public static void StableSortInPlace<T>(IList<T> list, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

System.Collections.Generic.IComparer<T> comparer

The comparer instance used to compare items in the collection. Only the Compare method is used.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

StableSortInPlace<T>(IList<T>, Comparison<T>)

Sorts a list or array in place. The sort is stable, which means that if items X and Y are equal, and X precedes Y in the unsorted collection, X will precede Y is the sorted collection. A supplied Comparison<T> delegate is used to compare the items in the list.

Declaration
public static void StableSortInPlace<T>(IList<T> list, Comparison<T> comparison)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list

The list or array to sort.

System.Comparison<T> comparison

The comparison delegate used to compare items in the collection.

Type Parameters
Name Description
T
Remarks

Although arrays cast to IList<T> are normally read-only, this method will work correctly and modify an array passed as list.

| Improve this Doc View Source

ToArray<T>(IEnumerable<T>)

Create an array with the items in a collection.

Declaration
public static T[] ToArray<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

Collection to create array from.

Returns
Type Description
T[]

An array with the items from the collection, in enumeration order.

Type Parameters
Name Description
T

Element type of the collection.

Remarks

If collection implements ICollection<T>T, then ICollection<T>.CopyTo() is used to fill the array. Otherwise, the IEnumerable<T>.GetEnumerator() is used to fill the array.

Exceptions
Type Condition
System.ArgumentNullException

collection is null.

| Improve this Doc View Source

ToString<T>(IEnumerable<T>)

Gets a string representation of the elements in the collection. The string representation starts with "{", has a list of items separated by commas (","), and ends with "}". Each item in the collection is converted to a string by calling its ToString method (null is represented by "null"). Contained collections (except strings) are recursively converted to strings by this method.

Declaration
public static string ToString<T>(IEnumerable<T> collection)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

A collection to get the string representation of.

Returns
Type Description
System.String

The string representation of the collection. If collection is null, then the string "null" is returned.

Type Parameters
Name Description
T
| Improve this Doc View Source

ToString<T>(IEnumerable<T>, Boolean, String, String, String)

Gets a string representation of the elements in the collection. The string to used at the beginning and end, and to separate items, and supplied by parameters. Each item in the collection is converted to a string by calling its ToString method (null is represented by "null").

Declaration
public static string ToString<T>(IEnumerable<T> collection, bool recursive, string start, string separator, string end)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

A collection to get the string representation of.

System.Boolean recursive

If true, contained collections (except strings) are converted to strings by a recursive call to this method, instead of by calling ToString.

System.String start

The string to appear at the beginning of the output string.

System.String separator

The string to appear between each item in the string.

System.String end

The string to appear at the end of the output string.

Returns
Type Description
System.String

The string representation of the collection. If collection is null, then the string "null" is returned.

Type Parameters
Name Description
T
Exceptions
Type Condition
System.ArgumentNullException

start, separator, or end is null.

| Improve this Doc View Source

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

Gets a string representation of the mappings in a dictionary. The string representation starts with "{", has a list of mappings separated by commas (", "), and ends with "}". Each mapping is represented by "key->value". Each key and value in the dictionary is converted to a string by calling its ToString method (null is represented by "null"). Contained collections (except strings) are recursively converted to strings by this method.

Declaration
public static string ToString<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
System.Collections.Generic.IDictionary<TKey, TValue> dictionary

A dictionary to get the string representation of.

Returns
Type Description
System.String

The string representation of the collection, or "null" if dictionary is null.

Type Parameters
Name Description
TKey
TValue
| Improve this Doc View Source

TrueForAll<T>(IEnumerable<T>, Predicate<T>)

Determines if all of the items in the collection satisfy the condition defined by predicate.

Declaration
public static bool TrueForAll<T>(IEnumerable<T> collection, Predicate<T> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to check all the items in.

System.Predicate<T> predicate

A delegate that defines the condition to check for.

Returns
Type Description
System.Boolean

True if all of the items in the collection satisfy the condition defined by predicate, or if the collection is empty. False if one or more items in the collection do not satisfy predicate.

Type Parameters
Name Description
T
| Improve this Doc View Source

TryFindFirstWhere<T>(IEnumerable<T>, Predicate<T>, out T)

Finds the first item in a collection that satisfies the condition defined by predicate.

Declaration
public static bool TryFindFirstWhere<T>(IEnumerable<T> collection, Predicate<T> predicate, out T foundItem)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

T foundItem

Outputs the first item in the collection that matches the condition, if the method returns true.

Returns
Type Description
System.Boolean

True if an item satisfying the condition was found. False if no such item exists in the collection.

Type Parameters
Name Description
T
See Also
FindFirstWhere<T>(IEnumerable<T>, Predicate<T>)
| Improve this Doc View Source

TryFindLastWhere<T>(IEnumerable<T>, Predicate<T>, out T)

Finds the last item in a collection that satisfies the condition defined by predicate.

Declaration
public static bool TryFindLastWhere<T>(IEnumerable<T> collection, Predicate<T> predicate, out T foundItem)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> collection

The collection to search.

System.Predicate<T> predicate

A delegate that defined the condition to check for.

T foundItem

Outputs the last item in the collection that matches the condition, if the method returns true.

Returns
Type Description
System.Boolean

True if an item satisfying the condition was found. False if no such item exists in the collection.

Type Parameters
Name Description
T
Remarks

If the collection implements IList<T>, then the list is scanned in reverse until a matching item is found. Otherwise, the entire collection is iterated in the forward direction.

See Also
FindLastWhere<T>(IEnumerable<T>, Predicate<T>)
| Improve this Doc View Source

TypedAs<T>(ICollection)

Given a non-generic ICollection interface, wrap a generic ICollection<T> interface around it. The generic interface will enumerate the same objects as the underlying non-generic collection, but can be used in places that require a generic interface. The underlying non-generic collection must contain only items that are of type T or a type derived from it. This method is useful when interfacing older, non-generic collections to newer code that uses generic interfaces.

Declaration
public static ICollection<T> TypedAs<T>(ICollection untypedCollection)
Parameters
Type Name Description
System.Collections.ICollection untypedCollection

An untyped collection. This collection should only contain items of type T or a type derived from it.

Returns
Type Description
System.Collections.Generic.ICollection<T>

A generic ICollection<T> wrapper around untypedCollection. If untypedCollection is null, then null is returned.

Type Parameters
Name Description
T

The item type of the wrapper collection.

Remarks

Some collections implement both generic and non-generic interfaces. For efficiency, this method will first attempt to cast untypedCollection to ICollection<T>. If that succeeds, it is returned; otherwise, a wrapper object is created.

Unlike the generic interface, the non-generic ICollection interfaces does not contain methods for adding or removing items from the collection. For this reason, the returned ICollection<T> will be read-only.

| Improve this Doc View Source

TypedAs<T>(IEnumerable)

Given a non-generic IEnumerable interface, wrap a generic IEnumerable<T> interface around it. The generic interface will enumerate the same objects as the underlying non-generic collection, but can be used in places that require a generic interface. The underlying non-generic collection must contain only items that are of type T or a type derived from it. This method is useful when interfacing older, non-generic collections to newer code that uses generic interfaces.

Declaration
public static IEnumerable<T> TypedAs<T>(IEnumerable untypedCollection)
Parameters
Type Name Description
System.Collections.IEnumerable untypedCollection

An untyped collection. This collection should only contain items of type T or a type derived from it.

Returns
Type Description
System.Collections.Generic.IEnumerable<T>

A generic IEnumerable<T> wrapper around untypedCollection. If untypedCollection is null, then null is returned.

Type Parameters
Name Description
T

The item type of the wrapper collection.

Remarks

Some collections implement both generic and non-generic interfaces. For efficiency, this method will first attempt to cast untypedCollection to IEnumerable<T>. If that succeeds, it is returned; otherwise, a wrapper object is created.

| Improve this Doc View Source

TypedAs<T>(IList)

Given a non-generic IList interface, wrap a generic IList<T> interface around it. The generic interface will enumerate the same objects as the underlying non-generic list, but can be used in places that require a generic interface. The underlying non-generic list must contain only items that are of type T or a type derived from it. This method is useful when interfacing older, non-generic lists to newer code that uses generic interfaces.

Declaration
public static IList<T> TypedAs<T>(IList untypedList)
Parameters
Type Name Description
System.Collections.IList untypedList

An untyped list. This list should only contain items of type T or a type derived from it.

Returns
Type Description
System.Collections.Generic.IList<T>

A generic IList<T> wrapper around untypedlist. If untypedlist is null, then null is returned.

Type Parameters
Name Description
T

The item type of the wrapper list.

Remarks

Some collections implement both generic and non-generic interfaces. For efficiency, this method will first attempt to cast untypedList to IList<T>. If that succeeds, it is returned; otherwise, a wrapper object is created.

| Improve this Doc View Source

Untyped<T>(ICollection<T>)

Given a generic ICollection<T> interface, wrap a non-generic (untyped) ICollection interface around it. The non-generic interface will contain the same objects as the underlying generic collection, but can be used in places that require a non-generic interface. This method is useful when interfacing generic interfaces with older code that uses non-generic interfaces.

Declaration
public static ICollection Untyped<T>(ICollection<T> typedCollection)
Parameters
Type Name Description
System.Collections.Generic.ICollection<T> typedCollection

A typed collection to wrap.

Returns
Type Description
System.Collections.ICollection

A non-generic ICollection wrapper around typedCollection. If typedCollection is null, then null is returned.

Type Parameters
Name Description
T

The item type of the underlying collection.

Remarks

Many generic collections already implement the non-generic interfaces directly. This method will first attempt to simply cast typedCollection to ICollection. If that succeeds, it is returned; if it fails, then a wrapper object is created.

| Improve this Doc View Source

Untyped<T>(IList<T>)

Given a generic IList<T> interface, wrap a non-generic (untyped) IList interface around it. The non-generic interface will contain the same objects as the underlying generic list, but can be used in places that require a non-generic interface. This method is useful when interfacing generic interfaces with older code that uses non-generic interfaces.

Declaration
public static IList Untyped<T>(IList<T> typedList)
Parameters
Type Name Description
System.Collections.Generic.IList<T> typedList

A typed list to wrap.

Returns
Type Description
System.Collections.IList

A non-generic IList wrapper around typedList. If typedList is null, then null is returned.

Type Parameters
Name Description
T

The item type of the underlying list.

Remarks

Many generic collections already implement the non-generic interfaces directly. This method will first attempt to simply cast typedList to IList. If that succeeds, it is returned; if it fails, then a wrapper object is created.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX