Class Enumerables
Functions for working with enumerables.
Inheritance
Inherited Members
Namespace: Virtlink.Utilib.Collections
Assembly: Virtlink.Utilib.dll
Syntax
public static class Enumerables
Methods
| Improve this Doc View SourceAsList<T>(IEnumerable<T>)
Returns the enumerable as a list, either by casting it or by wrapping it in a smart list.
Declaration
public static IReadOnlyList<T> AsList<T>(this IEnumerable<T> enumerable)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable | The enumerable to wrap. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<T> | The wrapped enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
Remarks
The smart list tries to not fully enumerate the given enumerable whenever possible, and will ensure the enumerable is enumerated only once.
CreateSafetyCopy<T>(IEnumerable<T>)
Creates a safety copy of the specified enumerable.
Declaration
public static IReadOnlyList<T> CreateSafetyCopy<T>(this IEnumerable<T> source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | The source enumerable. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IReadOnlyList<T> | The safety copy. |
Type Parameters
Name | Description |
---|---|
T | The type of elements. |
Remarks
Any changes to the source collection are not reflected in the resulting list.
Of<T>()
Returns an empty enumerable.
Declaration
public static IEnumerable<T> Of<T>()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The empty enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
Of<T>(T)
Returns a enumerable with one value.
Declaration
public static IEnumerable<T> Of<T>(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The value. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The singleton enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
Of<T>(T, T)
Returns a enumerable with two values.
Declaration
public static IEnumerable<T> Of<T>(T value1, T value2)
Parameters
Type | Name | Description |
---|---|---|
T | value1 | The first value. |
T | value2 | The second value. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
Of<T>(T, T, T)
Returns a enumerable with three values.
Declaration
public static IEnumerable<T> Of<T>(T value1, T value2, T value3)
Parameters
Type | Name | Description |
---|---|---|
T | value1 | The first value. |
T | value2 | The second value. |
T | value3 | The third value. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
Of<T>(T[])
Returns a enumerable with multiple values.
Declaration
public static IEnumerable<T> Of<T>(params T[] values)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | The values. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
OrIfEmpty<T>(IEnumerable<T>, IEnumerable<T>)
Returns the elements of the first sequence, or the elements of the second sequence when the first sequence is empty.
Declaration
public static IEnumerable<T> OrIfEmpty<T>(this IEnumerable<T> source, IEnumerable<T> otherwise)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | source | The first sequence. |
System.Collections.Generic.IEnumerable<T> | otherwise | The second sequence, only to be returned when the first sequence is empty. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<T> | The resulting enumerable. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the sequences. |
ToDictionary<TKey, TElement>(IEnumerable<KeyValuePair<TKey, TElement>>)
Returns a dictionary from a sequence of key-value-pairs.
Declaration
public static Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(this IEnumerable<KeyValuePair<TKey, TElement>> source)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TElement>> | source | The source sequence. |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, TElement> | The resulting dictionary. |
Type Parameters
Name | Description |
---|---|
TKey | The type of keys. |
TElement | The type of values. |
ToDictionary<TKey, TElement>(IEnumerable<KeyValuePair<TKey, TElement>>, IEqualityComparer<TKey>)
Returns a dictionary from a sequence of key-value-pairs.
Declaration
public static Dictionary<TKey, TElement> ToDictionary<TKey, TElement>(this IEnumerable<KeyValuePair<TKey, TElement>> source, IEqualityComparer<TKey> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TElement>> | source | The source sequence. |
System.Collections.Generic.IEqualityComparer<TKey> | comparer | The comparer to use; or null. |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<TKey, TElement> | The resulting dictionary. |
Type Parameters
Name | Description |
---|---|
TKey | The type of keys. |
TElement | The type of values. |
TryGetCount(IEnumerable)
Attempts to get the number of elements in the enumerable, only if it can be determined efficiently.
Declaration
public static int? TryGetCount(IEnumerable enumerable)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IEnumerable | enumerable | The enumerable for which tog et the count. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | The number of elements; or null when it could not be determined without enumerating all elements. |
TryGetCount<T>(IEnumerable<T>)
Attempts to get the number of elements in the enumerable, only if it can be determined efficiently.
Declaration
public static int? TryGetCount<T>(IEnumerable<T> enumerable)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T> | enumerable | The enumerable for which to get the count. |
Returns
Type | Description |
---|---|
System.Nullable<System.Int32> | The number of elements; or null when it could not be determined without enumerating all elements. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the enumerable. |
ZipEqual<T1, T2, TResult>(IEnumerable<T1>, IEnumerable<T2>, Func<T1, T2, TResult>)
Zips two sequences together, like Zip, but enforces that both sequences have equal lengths.
Declaration
public static IEnumerable<TResult> ZipEqual<T1, T2, TResult>(this IEnumerable<T1> first, IEnumerable<T2> second, Func<T1, T2, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<T1> | first | |
System.Collections.Generic.IEnumerable<T2> | second | |
System.Func<T1, T2, TResult> | resultSelector |
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TResult> | The resulting sequence. |
Type Parameters
Name | Description |
---|---|
T1 | The type of elements in the first sequence. |
T2 | The type of elements in the second sequence. |
TResult | The result selector. |