Table of Contents

Class CollectionExtensions

Namespace
Glitch9
public static class CollectionExtensions
Inheritance
CollectionExtensions
Inherited Members

Methods

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

Adds an element to the list if it does not already contain it, based on the specified equality comparer.

public static bool AddDistinct<T>(this IList<T> list, T value, IEqualityComparer<T> comparer)

Parameters

list IList<T>
value T
comparer IEqualityComparer<T>

Returns

bool

Type Parameters

T

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

public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue value)

Parameters

dict IDictionary<TKey, TValue>
key TKey
value TValue

Type Parameters

TKey
TValue

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

Adds a range of elements to the list, ensuring each element is added only once.

public static bool AddRangeDistinct<T>(this IList<T> list, IEnumerable<T> values, IEqualityComparer<T> comparer)

Parameters

list IList<T>
values IEnumerable<T>
comparer IEqualityComparer<T>

Returns

bool

Type Parameters

T

AddRange<T>(HashSet<T>, IEnumerable<T>)

Adds a collection of elements to a hash set.

public static void AddRange<T>(this HashSet<T> hashSet, IEnumerable<T> range)

Parameters

hashSet HashSet<T>
range IEnumerable<T>

Type Parameters

T

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

Adds the elements of the specified collection to the end of the IList<T>.

public static void AddRange<T>(this IList<T> list, IEnumerable<T> collection)

Parameters

list IList<T>
collection IEnumerable<T>

Type Parameters

T

ContainsValue<TSource>(IEnumerable<TSource>, TSource, IEqualityComparer<TSource>)

Determines whether the collection contains the specified value, using the specified equality comparer.

public static bool ContainsValue<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)

Parameters

source IEnumerable<TSource>
value TSource
comparer IEqualityComparer<TSource>

Returns

bool

Type Parameters

TSource

Contains<T>(List<T>, T, IEqualityComparer)

Determines whether the list contains the specified value, using the specified equality comparer.

public static bool Contains<T>(this List<T> list, T value, IEqualityComparer comparer)

Parameters

list List<T>
value T
comparer IEqualityComparer

Returns

bool

Type Parameters

T

GetBestMatch<T>(Dictionary<string, T>, string, Action<string>)

public static T GetBestMatch<T>(this Dictionary<string, T> dict, string key, Action<string> onNotFound = null)

Parameters

dict Dictionary<string, T>
key string
onNotFound Action<string>

Returns

T

Type Parameters

T

GetBestMatch<T>(Dictionary<string, T>, string, char, Action<string>)

public static T GetBestMatch<T>(this Dictionary<string, T> dict, string key, char separator, Action<string> onNotFound = null)

Parameters

dict Dictionary<string, T>
key string
separator char
onNotFound Action<string>

Returns

T

Type Parameters

T

GetRandom<T>(IList<T>)

Gets a random element from the list.

public static T GetRandom<T>(this IList<T> list)

Parameters

list IList<T>

Returns

T

Type Parameters

T

IndexOfReference<T>(List<T>, T)

Finds the index of the specified item in the list by reference equality.

public static int IndexOfReference<T>(this List<T> list, T item)

Parameters

list List<T>
item T

Returns

int

Type Parameters

T

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

Finds the index of the first element in the collection that matches the specified predicate.

public static int IndexOf<T>(this IEnumerable<T> collection, Func<T, bool> predicate)

Parameters

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

Returns

int

Type Parameters

T

IsDictionaryType(Type)

Determines whether the specified type is a dictionary type.

public static bool IsDictionaryType(Type type)

Parameters

type Type

Returns

bool

IsNotNullOrEmpty<T>(IEnumerable<T>)

Determines whether the collection is valid (not null and contains elements).

public static bool IsNotNullOrEmpty<T>(this IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

Returns

bool

Type Parameters

T

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

public static bool IsNotNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dict)

Parameters

dict IDictionary<TKey, TValue>

Returns

bool

Type Parameters

TKey
TValue

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

public static bool IsNotNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)

Parameters

dict IDictionary<TKey, TValue>
key TKey

Returns

bool

Type Parameters

TKey
TValue

IsNullOrEmpty<T>(IEnumerable<T>)

Determines whether the collection is null or empty.

public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

Returns

bool

Type Parameters

T

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

public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dict)

Parameters

dict IDictionary<TKey, TValue>

Returns

bool

Type Parameters

TKey
TValue

Populate<T>(IList<T>, T)

Sets all items in the list to the given value.

public static void Populate<T>(this IList<T> list, T item)

Parameters

list IList<T>
item T

Type Parameters

T

SetNullIfEmpty<T>(List<T>)

public static List<T> SetNullIfEmpty<T>(this List<T> list)

Parameters

list List<T>

Returns

List<T>

Type Parameters

T

Sort<T>(IList<T>)

Sorts an IList.

public static void Sort<T>(this IList<T> list)

Parameters

list IList<T>

Type Parameters

T

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

Sorts an IList using a specified comparison.

public static void Sort<T>(this IList<T> list, Comparison<T> comparison)

Parameters

list IList<T>
comparison Comparison<T>

Type Parameters

T

SwapElements<T>(IList<T>, int, int)

Swaps elements in a list at the specified indices.

public static void SwapElements<T>(this IList<T> list, int index1, int index2)

Parameters

list IList<T>
index1 int
index2 int

Type Parameters

T

ToMultidimensionalArray(IList, Type, int)

Converts a jagged array to a multidimensional array.

public static Array ToMultidimensionalArray(IList values, Type type, int rank)

Parameters

values IList
type Type
rank int

Returns

Array

ToStack<T>(IList<T>)

Converts a list to a stack.

public static Stack<T> ToStack<T>(this IList<T> list)

Parameters

list IList<T>

Returns

Stack<T>

Type Parameters

T

TryGet<T>(IList<T>, int)

Tries to get an element from the list at the specified index.

public static T TryGet<T>(this IList<T> list, int index)

Parameters

list IList<T>
index int

Returns

T

Type Parameters

T