Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Programming · C# · Chapter 06 / 10

Programming learning path

Collections, generics & IEnumerable

Generic collections give type-safe containers. The most common choices solve different access patterns rather than being interchangeable containers.

Common collections

  • List<T>: ordered resizable sequence.
  • Dictionary<TKey,TValue>: key-to-value lookup with unique keys.
  • HashSet<T>: unique values and set operations.
var tests = new List<string> { "login", "checkout" };
var durations = new Dictionary<string, int>
{
    ["login"] = 120,
    ["checkout"] = 350
};

var tags = new HashSet<string> { "smoke", "api", "smoke" };

Generics

Generics let a type or method work with different element types while keeping compile-time type safety. List<string> and List<int> reuse the same generic implementation without falling back to object and repeated casts.

IEnumerable<T>

IEnumerable<T> means that a sequence can yield elements one by one. It does not promise indexing, mutability or even a known count. A sequence can also execute work each time it is enumerated.

IEnumerable<string> names = tests;

foreach (var test in names)
{
    Console.WriteLine(test);
}

Key points

  • Choose a collection according to the operation you need: order, key lookup or uniqueness.
  • Generics provide reuse with compile-time type safety.
  • IEnumerable<T> is an enumeration contract, not a guarantee of an in-memory list.

Source registry

2 chapter references
Collections and data structures

Collection families, generic collections and common selection trade-offs

Microsoft Learn · Official documentation
Source ↗
Generics

Generic types, methods and type parameters

Microsoft Learn · Official documentation
Source ↗