Roadmap › C# Language — Basics to Advanced › Module: Arrays and Collections
Lessons in this course (38)

10. Collections overview: generic vs non-generic

Basic★ Must knowLesson 10 of 38
TypeWhen to useLookup
T[]Fixed sizeO(1) by index
List<T>Growable listO(1) index, O(n) search
Dictionary<TKey,TValue>Key → valueO(1) average
HashSet<T>Unique items, fast ContainsO(1) average
Queue<T> / Stack<T>FIFO / LIFOO(1) enqueue/push
LinkedList<T>Many inserts/removes in the middleO(1) insert with a node
SortedDictionary / SortedListOrdered keysO(log n)
ConcurrentDictionary, ConcurrentQueueMulti-threaded accessthread-safe
ImmutableList, FrozenDictionary (.NET 8)Read-only shared data—
csharp
var prices = new Dictionary<string, decimal> { ["pen"] = 10m, ["book"] = 250m };
if (prices.TryGetValue("pen", out var p)) Console.WriteLine(p);

int[] nums = [5, 3, 8];          // collection expression (C# 12)
List<int> list = [.. nums, 10];   // spread
  • IEnumerable<T> = can be iterated; ICollection<T> adds Count/Add/Remove; IList<T> adds index access.
  • Prefer returning IReadOnlyList<T>/IEnumerable<T> from public APIs.

Generic vs non-generic collections:

Non-generic (System.Collections, .NET 1.x)Generic replacement (System.Collections.Generic, .NET 2.0+)
ArrayListList<T>
HashtableDictionary<TKey, TValue>
SortedListSortedList<TKey, TValue>
Stack, QueueStack<T>, Queue<T>

Generic collections are type-safe (compile-time errors instead of runtime InvalidCastException) and faster for value types (no boxing). Use the non-generic ones only when maintaining old code.