Skip to content

EN CSharp Collections

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

List, Dictionary, HashSet, Queues and Stacks

Manual C# · Examples UO

Collections are selected by operations: List<T> stores an ordered mutable list, Dictionary<TKey,TValue> - values ​​by key, HashSet<T> - unique values, Queue<T> - order FIFO, Stack<T> - LIFO. The generic parameter specifies the element type.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
var counts = new System.Collections.Generic.List<int> { 2, 3 };
counts.Add(4);
System.Console.WriteLine(counts.Count);

Result:

3
var visited = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
visited.Add("backpack");
visited.Add("BACKPACK");
UO.Print($"Unique names: {visited.Count}");
var delays = new Dictionary<string, int>
{
    ["short"] = 100,
    ["long"] = 500
};
if (delays.TryGetValue("short", out int delay))
{
    await WaitAsync(delay);
}
var queue = new Queue<string>();
queue.Enqueue("first");
queue.Enqueue("second");
UO.Print(queue.Dequeue());

Do not modify a regular collection during its foreach without a suitable algorithm. For simultaneous operation of threads, there are separate concurrent collections; they do not make game operations thread safe on their own.

Official Documentation: List, Dictionary, HashSet, Queues and Stacks - Microsoft Learn.

Clone this wiki locally