VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/generics-collections-in-c/

⇱ Generics Collections in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generics Collections in C#

Last Updated : 20 Apr, 2026

A generic collection in C# is a collection class that can store objects of a specific type, specified when the collection is created. Unlike non-generic collections, generic collections enforce type safety at compile time, preventing invalid types from being added and improve performance by eliminating the need for boxing/unboxing for value types.

Common Generic Collections in C#

1. List<T>

A dynamic array that automatically resizes as items are added. Provides indexed access, searching and sorting.

  • Maintains order of elements
  • Dynamic resizing
  • Supports LINQ operations

2. Dictionary<TKey, TValue>

Stores key-value pairs for fast lookups by key.

  • Fast retrieval using keys
  • Prevents duplicate keys
  • Ideal for mapping relationships

3. Queue<T>

Represents a first-in, first-out (FIFO) collection.

  • Maintains insertion order
  • Suitable for task scheduling or processing

4. Stack<T>

Represents a last-in, first-out (LIFO) collection.

  • Last element added is the first to be removed
  • Useful for undo functionality, parsing and recursion simulations

5. HashSet<T>

Stores unique elements without duplicates.

  • Ensures uniqueness of elements
  • Supports set operations like Union, Intersection and Difference

6. SortedList<TKey, TValue>

Stores key-value pairs sorted by key automatically.

  • Maintains sorted order of keys
  • Quick retrieval by key
  • Useful for ordered mappings

Choosing the Right Collection

  • Use List<T> for dynamic arrays and ordered lists.
  • Use Dictionary<TKey, TValue> for key-value mappings with fast lookups.
  • Use Queue<T> and Stack<T> when order of insertion or removal matters.
  • Use HashSet<T> to ensure element uniqueness.
  • Use SortedList<TKey, TValue> when you need automatically sorted key-value pairs.
Comment
Article Tags:

Explore