Skip to content

OnixLabs.Core.Collections.Collection

matthew edited this page May 27, 2026 · 1 revision

Collection Class

Provides methods to create empty or populated mutable and immutable collections, using a single, uniform naming convention across all standard collection types.

Namespace OnixLabs.Core.Collections
Assembly OnixLabs.Core.dll (NuGet)
Inheritance object → Collection

Definition

public static partial class Collection

Remarks

Collection exists to make collection construction read consistently regardless of the target type. Each factory comes in two flavours:

  • An Empty… method that produces an empty collection of the named type.
  • An …Of method that produces a collection of the named type populated with the supplied items.

The factory methods are thin wrappers that defer to collection-expression initializers, copy constructors, or the corresponding Immutable*.CreateRange builders. Immutable empties reuse the canonical shared empty instance where one exists (for example, ImmutableDictionary<TKey, TValue>.Empty).

The Dictionary/SortedDictionary/ImmutableDictionary/ImmutableSortedDictionary factories constrain TKey with where TKey : notnull.

Examples

Creating empty and populated collections

using OnixLabs.Core.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;

List<int> emptyList = Collection.EmptyList<int>();
int[] numbers = Collection.ArrayOf(1, 2, 3);

ImmutableList<string> names = Collection.ImmutableListOf("Alice", "Bob");
HashSet<int> set = Collection.HashSetOf(1, 1, 2, 3); // { 1, 2, 3 }

Building dictionaries from tuples or key/value pairs

using OnixLabs.Core.Collections;
using System.Collections.Generic;

Dictionary<string, int> fromTuples = Collection.DictionaryOf(
    ("one", 1),
    ("two", 2));

Dictionary<string, int> fromPairs = Collection.DictionaryOf(
    new KeyValuePair<string, int>("three", 3));

Empty Factory Methods

Each method returns a new, empty instance of the named collection type.

Method Returns Description
EmptyEnumerable<T>() IEnumerable<T> Creates an empty enumerable.
EmptyArray<T>() T[] Creates an empty array.
EmptyImmutableArray<T>() ImmutableArray<T> Creates an empty immutable array.
EmptyList<T>() List<T> Creates an empty list.
EmptyImmutableList<T>() ImmutableList<T> Creates an empty immutable list.
EmptyDictionary<TKey, TValue>() Dictionary<TKey, TValue> Creates an empty dictionary.
EmptyImmutableDictionary<TKey, TValue>() ImmutableDictionary<TKey, TValue> Creates an empty immutable dictionary.
EmptySortedDictionary<TKey, TValue>() SortedDictionary<TKey, TValue> Creates an empty sorted dictionary.
EmptyImmutableSortedDictionary<TKey, TValue>() ImmutableSortedDictionary<TKey, TValue> Creates an empty immutable sorted dictionary.
EmptyHashSet<T>() HashSet<T> Creates an empty hash set.
EmptyImmutableHashSet<T>() ImmutableHashSet<T> Creates an empty immutable hash set.
EmptySortedSet<T>() SortedSet<T> Creates an empty sorted set.
EmptyImmutableSortedSet<T>() ImmutableSortedSet<T> Creates an empty immutable sorted set.
EmptyStack<T>() Stack<T> Creates an empty stack.
EmptyImmutableStack<T>() ImmutableStack<T> Creates an empty immutable stack.
EmptyQueue<T>() Queue<T> Creates an empty queue.
EmptyImmutableQueue<T>() ImmutableQueue<T> Creates an empty immutable queue.

EmptyEnumerable<T>()

public static IEnumerable<T> EmptyEnumerable<T>()

Creates an empty IEnumerable<T>.

Type Parameter Description
T The underlying type of the enumerable.

Returns — An empty enumerable.

EmptyArray<T>()

public static T[] EmptyArray<T>()

Creates an empty T array.

Type Parameter Description
T The underlying type of the array.

Returns — An empty array.

EmptyImmutableArray<T>()

public static ImmutableArray<T> EmptyImmutableArray<T>()

Creates an empty ImmutableArray<T>.

Type Parameter Description
T The underlying type of the immutable array.

Returns — An empty immutable array.

EmptyList<T>()

public static List<T> EmptyList<T>()

Creates an empty List<T>.

Type Parameter Description
T The underlying type of the list.

Returns — An empty list.

EmptyImmutableList<T>()

public static ImmutableList<T> EmptyImmutableList<T>()

Creates an empty ImmutableList<T>.

Type Parameter Description
T The underlying type of the immutable list.

Returns — An empty immutable list.

EmptyDictionary<TKey, TValue>()

public static Dictionary<TKey, TValue> EmptyDictionary<TKey, TValue>() where TKey : notnull

Creates an empty Dictionary<TKey, TValue>.

Type Parameter Description
TKey The underlying type of the dictionary key.
TValue The underlying type of the dictionary value.

Returns — An empty dictionary.

EmptyImmutableDictionary<TKey, TValue>()

public static ImmutableDictionary<TKey, TValue> EmptyImmutableDictionary<TKey, TValue>() where TKey : notnull

Creates an empty ImmutableDictionary<TKey, TValue>.

Type Parameter Description
TKey The underlying type of the immutable dictionary key.
TValue The underlying type of the immutable dictionary value.

Returns — An empty immutable dictionary.

EmptySortedDictionary<TKey, TValue>()

public static SortedDictionary<TKey, TValue> EmptySortedDictionary<TKey, TValue>() where TKey : notnull

Creates an empty SortedDictionary<TKey, TValue>.

Type Parameter Description
TKey The underlying type of the sorted dictionary key.
TValue The underlying type of the sorted dictionary value.

Returns — An empty sorted dictionary.

EmptyImmutableSortedDictionary<TKey, TValue>()

public static ImmutableSortedDictionary<TKey, TValue> EmptyImmutableSortedDictionary<TKey, TValue>() where TKey : notnull

Creates an empty ImmutableSortedDictionary<TKey, TValue>.

Type Parameter Description
TKey The underlying type of the immutable sorted dictionary key.
TValue The underlying type of the immutable sorted dictionary value.

Returns — An empty immutable sorted dictionary.

EmptyHashSet<T>()

public static HashSet<T> EmptyHashSet<T>()

Creates an empty HashSet<T>.

Type Parameter Description
T The underlying type of the hash set.

Returns — An empty hash set.

EmptyImmutableHashSet<T>()

public static ImmutableHashSet<T> EmptyImmutableHashSet<T>()

Creates an empty ImmutableHashSet<T>.

Type Parameter Description
T The underlying type of the immutable hash set.

Returns — An empty immutable hash set.

EmptySortedSet<T>()

public static SortedSet<T> EmptySortedSet<T>()

Creates an empty SortedSet<T>.

Type Parameter Description
T The underlying type of the sorted set.

Returns — An empty sorted set.

EmptyImmutableSortedSet<T>()

public static ImmutableSortedSet<T> EmptyImmutableSortedSet<T>()

Creates an empty ImmutableSortedSet<T>.

Type Parameter Description
T The underlying type of the immutable sorted set.

Returns — An empty immutable sorted set.

EmptyStack<T>()

public static Stack<T> EmptyStack<T>()

Creates an empty Stack<T>.

Type Parameter Description
T The underlying type of the stack.

Returns — An empty stack.

EmptyImmutableStack<T>()

public static ImmutableStack<T> EmptyImmutableStack<T>()

Creates an empty ImmutableStack<T>.

Type Parameter Description
T The underlying type of the immutable stack.

Returns — An empty immutable stack.

EmptyQueue<T>()

public static Queue<T> EmptyQueue<T>()

Creates an empty Queue<T>.

Type Parameter Description
T The underlying type of the queue.

Returns — An empty queue.

EmptyImmutableQueue<T>()

public static ImmutableQueue<T> EmptyImmutableQueue<T>()

Creates an empty ImmutableQueue<T>.

Type Parameter Description
T The underlying type of the immutable queue.

Returns — An empty immutable queue.

Of Factory Methods

Each method returns a new instance of the named collection type populated with the supplied items.

Method Returns Description
EnumerableOf<T>(params IEnumerable<T>) IEnumerable<T> Creates an enumerable of the specified items.
ArrayOf<T>(params T[]) T[] Creates an array of the specified items.
ImmutableArrayOf<T>(params ImmutableArray<T>) ImmutableArray<T> Creates an immutable array of the specified items.
ListOf<T>(params List<T>) List<T> Creates a list of the specified items.
ImmutableListOf<T>(params ImmutableList<T>) ImmutableList<T> Creates an immutable list of the specified items.
HashSetOf<T>(params HashSet<T>) HashSet<T> Creates a hash set of the specified items.
ImmutableHashSetOf<T>(params ImmutableHashSet<T>) ImmutableHashSet<T> Creates an immutable hash set of the specified items.
SortedSetOf<T>(params SortedSet<T>) SortedSet<T> Creates a sorted set of the specified items.
ImmutableSortedSetOf<T>(params ImmutableSortedSet<T>) ImmutableSortedSet<T> Creates an immutable sorted set of the specified items.
StackOf<T>(params IEnumerable<T>) Stack<T> Creates a stack of the specified items.
ImmutableStackOf<T>(params ImmutableStack<T>) ImmutableStack<T> Creates an immutable stack of the specified items.
QueueOf<T>(params IEnumerable<T>) Queue<T> Creates a queue of the specified items.
ImmutableQueueOf<T>(params ImmutableQueue<T>) ImmutableQueue<T> Creates an immutable queue of the specified items.
DictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>) Dictionary<TKey, TValue> Creates a dictionary from key/value pairs.
DictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>) Dictionary<TKey, TValue> Creates a dictionary from key/value tuples.
ImmutableDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>) ImmutableDictionary<TKey, TValue> Creates an immutable dictionary from key/value pairs.
ImmutableDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>) ImmutableDictionary<TKey, TValue> Creates an immutable dictionary from key/value tuples.
SortedDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>) SortedDictionary<TKey, TValue> Creates a sorted dictionary from key/value pairs.
SortedDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>) SortedDictionary<TKey, TValue> Creates a sorted dictionary from key/value tuples.
ImmutableSortedDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>) ImmutableSortedDictionary<TKey, TValue> Creates an immutable sorted dictionary from key/value pairs.
ImmutableSortedDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>) ImmutableSortedDictionary<TKey, TValue> Creates an immutable sorted dictionary from key/value tuples.

All dictionary factories constrain TKey with where TKey : notnull.


EnumerableOf<T>(params IEnumerable<T>)

public static IEnumerable<T> EnumerableOf<T>(params IEnumerable<T> items)

Creates an IEnumerable<T> of the specified items.

Parameter Type Description
items params IEnumerable<T> The items which will populate the enumerable.

Returns — An enumerable populated with the specified items.

ArrayOf<T>(params T[])

public static T[] ArrayOf<T>(params T[] items)

Creates a T array of the specified items.

Parameter Type Description
items params T[] The items which will populate the array.

Returns — An array populated with the specified items.

ImmutableArrayOf<T>(params ImmutableArray<T>)

public static ImmutableArray<T> ImmutableArrayOf<T>(params ImmutableArray<T> items)

Creates an ImmutableArray<T> of the specified items.

Parameter Type Description
items params ImmutableArray<T> The items which will populate the immutable array.

Returns — An immutable array populated with the specified items.

ListOf<T>(params List<T>)

public static List<T> ListOf<T>(params List<T> items)

Creates a List<T> of the specified items.

Parameter Type Description
items params List<T> The items which will populate the list.

Returns — A list populated with the specified items.

ImmutableListOf<T>(params ImmutableList<T>)

public static ImmutableList<T> ImmutableListOf<T>(params ImmutableList<T> items)

Creates an ImmutableList<T> of the specified items.

Parameter Type Description
items params ImmutableList<T> The items which will populate the immutable list.

Returns — An immutable list populated with the specified items.

HashSetOf<T>(params HashSet<T>)

public static HashSet<T> HashSetOf<T>(params HashSet<T> items)

Creates a HashSet<T> of the specified items.

Parameter Type Description
items params HashSet<T> The items which will populate the hash set.

Returns — A hash set populated with the specified items.

ImmutableHashSetOf<T>(params ImmutableHashSet<T>)

public static ImmutableHashSet<T> ImmutableHashSetOf<T>(params ImmutableHashSet<T> items)

Creates an ImmutableHashSet<T> of the specified items.

Parameter Type Description
items params ImmutableHashSet<T> The items which will populate the immutable hash set.

Returns — An immutable hash set populated with the specified items.

SortedSetOf<T>(params SortedSet<T>)

public static SortedSet<T> SortedSetOf<T>(params SortedSet<T> items)

Creates a SortedSet<T> of the specified items.

Parameter Type Description
items params SortedSet<T> The items which will populate the sorted set.

Returns — A sorted set populated with the specified items.

ImmutableSortedSetOf<T>(params ImmutableSortedSet<T>)

public static ImmutableSortedSet<T> ImmutableSortedSetOf<T>(params ImmutableSortedSet<T> items)

Creates an ImmutableSortedSet<T> of the specified items.

Parameter Type Description
items params ImmutableSortedSet<T> The items which will populate the immutable sorted set.

Returns — An immutable sorted set populated with the specified items.

StackOf<T>(params IEnumerable<T>)

public static Stack<T> StackOf<T>(params IEnumerable<T> items)

Creates a Stack<T> of the specified items.

Parameter Type Description
items params IEnumerable<T> The items which will populate the stack.

Returns — A stack populated with the specified items.

ImmutableStackOf<T>(params ImmutableStack<T>)

public static ImmutableStack<T> ImmutableStackOf<T>(params ImmutableStack<T> items)

Creates an ImmutableStack<T> of the specified items.

Parameter Type Description
items params ImmutableStack<T> The items which will populate the immutable stack.

Returns — An immutable stack populated with the specified items.

QueueOf<T>(params IEnumerable<T>)

public static Queue<T> QueueOf<T>(params IEnumerable<T> items)

Creates a Queue<T> of the specified items.

Parameter Type Description
items params IEnumerable<T> The items which will populate the queue.

Returns — A queue populated with the specified items.

ImmutableQueueOf<T>(params ImmutableQueue<T>)

public static ImmutableQueue<T> ImmutableQueueOf<T>(params ImmutableQueue<T> items)

Creates an ImmutableQueue<T> of the specified items.

Parameter Type Description
items params ImmutableQueue<T> The items which will populate the immutable queue.

Returns — An immutable queue populated with the specified items.

DictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>)

public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(
    params IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull

Creates a Dictionary<TKey, TValue> of the specified items.

Parameter Type Description
items params IEnumerable<KeyValuePair<TKey, TValue>> The items which will populate the dictionary.

Returns — A dictionary populated with the specified items.

DictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>)

public static Dictionary<TKey, TValue> DictionaryOf<TKey, TValue>(
    params IEnumerable<(TKey key, TValue value)> items) where TKey : notnull

Creates a Dictionary<TKey, TValue> of the specified key/value tuples.

Parameter Type Description
items params IEnumerable<(TKey key, TValue value)> The items which will populate the dictionary.

Returns — A dictionary populated with the specified items.

ImmutableDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>)

public static ImmutableDictionary<TKey, TValue> ImmutableDictionaryOf<TKey, TValue>(
    params IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull

Creates an ImmutableDictionary<TKey, TValue> of the specified items.

Parameter Type Description
items params IEnumerable<KeyValuePair<TKey, TValue>> The items which will populate the immutable dictionary.

Returns — An immutable dictionary populated with the specified items.

ImmutableDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>)

public static ImmutableDictionary<TKey, TValue> ImmutableDictionaryOf<TKey, TValue>(
    params IEnumerable<(TKey key, TValue value)> items) where TKey : notnull

Creates an ImmutableDictionary<TKey, TValue> of the specified key/value tuples.

Parameter Type Description
items params IEnumerable<(TKey key, TValue value)> The items which will populate the immutable dictionary.

Returns — An immutable dictionary populated with the specified items.

SortedDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>)

public static SortedDictionary<TKey, TValue> SortedDictionaryOf<TKey, TValue>(
    params IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull

Creates a SortedDictionary<TKey, TValue> of the specified items.

Parameter Type Description
items params IEnumerable<KeyValuePair<TKey, TValue>> The items which will populate the sorted dictionary.

Returns — A sorted dictionary populated with the specified items.

SortedDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>)

public static SortedDictionary<TKey, TValue> SortedDictionaryOf<TKey, TValue>(
    params IEnumerable<(TKey key, TValue value)> items) where TKey : notnull

Creates a SortedDictionary<TKey, TValue> of the specified key/value tuples.

Parameter Type Description
items params IEnumerable<(TKey key, TValue value)> The items which will populate the sorted dictionary.

Returns — A sorted dictionary populated with the specified items.

ImmutableSortedDictionaryOf<TKey, TValue>(params IEnumerable<KeyValuePair<TKey, TValue>>)

public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryOf<TKey, TValue>(
    params IEnumerable<KeyValuePair<TKey, TValue>> items) where TKey : notnull

Creates an ImmutableSortedDictionary<TKey, TValue> of the specified items.

Parameter Type Description
items params IEnumerable<KeyValuePair<TKey, TValue>> The items which will populate the immutable sorted dictionary.

Returns — An immutable sorted dictionary populated with the specified items.

ImmutableSortedDictionaryOf<TKey, TValue>(params IEnumerable<(TKey, TValue)>)

public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionaryOf<TKey, TValue>(
    params IEnumerable<(TKey key, TValue value)> items) where TKey : notnull

Creates an ImmutableSortedDictionary<TKey, TValue> of the specified key/value tuples.

Parameter Type Description
items params IEnumerable<(TKey key, TValue value)> The items which will populate the immutable sorted dictionary.

Returns — An immutable sorted dictionary populated with the specified items.

See Also

ONIXLabs .NET Library


Clone this wiki locally