Repository navigation
EN CSharp Collections
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
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.
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.
ClassicUO — Bad Newbie & C# Scripts IDE