Table of Contents

Class ToCollectionExtensions

Namespace
Garyon.Extensions
Assembly
Garyon.dll

Contains extensions related to converting IEnumerable<T> instances into collections.

public static class ToCollectionExtensions
Inheritance
ToCollectionExtensions
Inherited Members

Methods

ToArrayOrEmpty<T>(IEnumerable<T>?)

Converts a provided sequence of elements into an array.

public static T[] ToArrayOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source IEnumerable<T> to convert into an array.

Returns

T[]

A newly created array containing the source sequence of elements, if not null, otherwise Empty<T>().

Type Parameters

T

The type of elements stored in the IEnumerable<T>.

ToArrayOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into an array of elements of the given source type, otherwise creates a new array and returns it.

public static T[] ToArrayOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

T[]

The source collection as an array of elements, if it was already, or a new array containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned array.

ToCollectionOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into an ICollection<T>, otherwise creates a new array and returns it as ICollection<T>.

public static ICollection<T> ToCollectionOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

ICollection<T>

The source collection as ICollection<T>, if it was already, or a new array containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned collection.

ToDictionaryOrExisting<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

Attempts to upcast the given source into a IDictionary<TKey, TValue>, otherwise creates a new collection and returns it as IDictionary<TKey, TValue>.

public static IDictionary<TKey, TValue> ToDictionaryOrExisting<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) where TKey : notnull

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The source of elements.

Returns

IDictionary<TKey, TValue>

The source collection as IDictionary<TKey, TValue>, if it was already, or a new collection containing the enumerated values of the source.

Type Parameters

TKey

The type of keys contained in the source and the returned dictionary.

TValue

The type of values contained in the source and the returned dictionary.

ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) where TKey : notnull

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

Returns

Dictionary<TKey, TValue>

Type Parameters

TKey
TValue

ToHashSetOrEmpty<T>(IEnumerable<T>?)

Converts a provided sequence of elements into a HashSet<T>.

public static HashSet<T> ToHashSetOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source IEnumerable<T> to convert into a HashSet<T>.

Returns

HashSet<T>

A newly created HashSet<T> containing the source sequence of elements, if not null, otherwise an empty set.

Type Parameters

T

The type of elements stored in the IEnumerable<T>.

ToHashSetOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a set of elements of the given source type, otherwise creates a new set and returns it.

public static HashSet<T> ToHashSetOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

HashSet<T>

The source collection as a set of elements, if it was already, or a new set containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned set.

ToListOrEmpty<T>(IEnumerable<T>?)

Converts a provided sequence of elements into a List<T>.

public static List<T> ToListOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source IEnumerable<T> to convert into a List<T>.

Returns

List<T>

A newly created List<T> containing the source sequence of elements, if not null, otherwise an empty list.

Type Parameters

T

The type of elements stored in the IEnumerable<T>.

ToListOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a list of elements of the given source type, otherwise creates a new list and returns it.

public static List<T> ToListOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

List<T>

The source collection as a list of elements, if it was already, or a new list containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned list.

ToReadOnlyCollectionOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a IReadOnlyCollection<T>, otherwise creates a new collection and returns it as IReadOnlyCollection<T>.

public static IReadOnlyCollection<T> ToReadOnlyCollectionOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

IReadOnlyCollection<T>

The source collection as IReadOnlyCollection<T>, if it was already, or a new collection containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned collection.

ToReadOnlyDictionaryOrExisting<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

Attempts to upcast the given source into a IReadOnlyDictionary<TKey, TValue>, otherwise creates a new collection and returns it as IReadOnlyDictionary<TKey, TValue>.

public static IReadOnlyDictionary<TKey, TValue> ToReadOnlyDictionaryOrExisting<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) where TKey : notnull

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The source of elements.

Returns

IReadOnlyDictionary<TKey, TValue>

The source collection as IReadOnlyDictionary<TKey, TValue>, if it was already, or a new collection containing the enumerated values of the source.

Type Parameters

TKey

The type of keys contained in the source and the returned dictionary.

TValue

The type of values contained in the source and the returned dictionary.

ToReadOnlyListOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a IReadOnlyList<T>, otherwise creates a new collection and returns it as IReadOnlyList<T>.

public static IReadOnlyList<T> ToReadOnlyListOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

IReadOnlyList<T>

The source collection as IReadOnlyList<T>, if it was already, or a new collection containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned list.

ToReadOnlySetOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a IReadOnlySet<T>, otherwise creates a new collection and returns it as IReadOnlySet<T>.

public static IReadOnlySet<T> ToReadOnlySetOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

IReadOnlySet<T>

The source collection as IReadOnlySet<T>, if it was already, or a new collection containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned set.

ToSetOrExisting<T>(IEnumerable<T>)

Attempts to upcast the given source into a ISet<T>, otherwise creates a new HashSet<T> and returns it as ISet<T>.

public static ISet<T> ToSetOrExisting<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source of elements.

Returns

ISet<T>

The source collection as ISet<T>, if it was already, or a new HashSet<T> containing the enumerated values of the source.

Type Parameters

T

The type of elements contained in the source and the returned set.

ToSortedDictionaryOrEmpty<TKey, TValue>(IDictionary<TKey, TValue>?)

Converts a provided sequence of elements into a SortedDictionary<TKey, TValue>.

public static SortedDictionary<TKey, TValue> ToSortedDictionaryOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue>? source) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source IDictionary<TKey, TValue> to convert into a SortedDictionary<TKey, TValue>.

Returns

SortedDictionary<TKey, TValue>

A newly created SortedDictionary<TKey, TValue> containing the source sequence of elements, if not null, otherwise an empty dictionary.

Type Parameters

TKey

The type of keys stored in the IDictionary<TKey, TValue>.

TValue

The type of values stored in the IDictionary<TKey, TValue>.

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

public static SortedDictionary<TKey, TValue> ToSortedDictionary<TKey, TValue>(this IDictionary<TKey, TValue> source) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

Returns

SortedDictionary<TKey, TValue>

Type Parameters

TKey
TValue

ToSortedListOrEmpty<TKey, TValue>(IDictionary<TKey, TValue>?)

Converts a provided sequence of elements into a SortedList<TKey, TValue>.

public static SortedList<TKey, TValue> ToSortedListOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue>? source) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

The source IDictionary<TKey, TValue> to convert into a SortedList<TKey, TValue>.

Returns

SortedList<TKey, TValue>

A newly created SortedList<TKey, TValue> containing the source sequence of elements, if not null, otherwise an empty dictionary.

Type Parameters

TKey

The type of keys stored in the IDictionary<TKey, TValue>.

TValue

The type of values stored in the IDictionary<TKey, TValue>.

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

public static SortedList<TKey, TValue> ToSortedList<TKey, TValue>(this IDictionary<TKey, TValue> source) where TKey : notnull

Parameters

source IDictionary<TKey, TValue>

Returns

SortedList<TKey, TValue>

Type Parameters

TKey
TValue

ToSortedSetOrEmpty<T>(IEnumerable<T>?)

Converts a provided sequence of elements into a SortedSet<T>.

public static SortedSet<T> ToSortedSetOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source IEnumerable<T> to convert into a SortedSet<T>.

Returns

SortedSet<T>

A newly created SortedSet<T> containing the source sequence of elements, if not null, otherwise an empty set.

Type Parameters

T

The type of elements stored in the IEnumerable<T>.

ToSortedSet<T>(IEnumerable<T>)

public static SortedSet<T> ToSortedSet<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

Returns

SortedSet<T>

Type Parameters

T