ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C# Tutorial• 6 min read

Collections & Generics: List<T> & Dictionary<K, V>

Type safety without boxing/unboxing overhead, HashSet<T>, and queues

Generic collections (System.Collections.Generic) provide high-performance, strongly typed collections that eliminate runtime typecasting and expensive boxing/unboxing overhead.

Key Generic Collections

List<T>: Strongly-typed resizable array with Add(), Remove(), and index access.

Dictionary<TKey, TValue>: Fast hash-table key-value store with ContainsKey() and TryGetValue().

HashSet<T>: Mathematical set guaranteeing distinct uniqueness.

Working with Generic Collections in C#
using System.Collections.Generic;

var trackList = new List<string> { "C#", "ASP.NET", "Azure" };
trackList.Add("Entity Framework");

var trackDurations = new Dictionary<string, int> {
    ["C#"] = 10,
    ["ASP.NET"] = 10,
    ["Azure"] = 8
};

if (trackDurations.TryGetValue("C#", out int lessons)) {
    Console.WriteLine($"C# Track has {lessons} lessons.");
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Why are Generic collections like `List<T>` preferred over legacy non-generic `ArrayList` in C#?