CHAPTER 20
Beginner
Generics and Collections in C#
Updated: May 17, 2026
5 min read
# CHAPTER 20
Generics and Collections
1. Introduction
In Chapter 9, we learned aboutList<T>. Have you ever wondered what the <T> means? It stands for Type. It is a feature called Generics. Generics allow you to design classes and methods where the data type is a variable to be specified later. This is what enables a single List class to hold ints, strings, or custom Player objects without rewriting the class!
2. Learning Objectives
By the end of this chapter, you will be able to:-
Understand the concept of Generics (
<T>).
- Write your own Generic Methods and Classes.
-
Use
Dictionary<TKey, TValue>for key-value pairs.
-
Use
Queue<T>(First-In, First-Out).
-
Use
Stack<T>(Last-In, First-Out).
3. Generic Methods
Imagine writing a method that swaps two integers. Then you need to swap two strings. Without generics, you have to write two overloaded methods. With generics, you write it *once*.
csharp
4. Generic Classes
Just like methods, entire classes can be generic. This is exactly how Microsoft builtList<T>.
csharp
5. Advanced Collections: Dictionary<TKey, TValue>
A dictionary stores data in Key-Value pairs. It is incredibly fast for looking up data (like a real-world dictionary).
*(Requires using System.Collections.Generic;)*
csharp
6. Advanced Collections: Queue<T>
A Queue operates on the FIFO (First-In, First-Out) principle. Think of a line at a grocery store. The first person in line is the first person served.
csharp
7. Advanced Collections: Stack<T>
A Stack operates on the LIFO (Last-In, First-Out) principle. Think of a stack of plates. The last plate you put on top is the first one you take off. (This is exactly how the call stack manages memory!).
csharp
8. Common Mistakes
-
Accessing a missing Dictionary key: If you ask for
ages["Charlie"]but "Charlie" isn't in the dictionary, the program will crash. Always check first usingif (ages.ContainsKey("Charlie")).
-
Using non-generic collections: Avoid
ArrayListandHashtable. Always use their modern generic equivalents:List<T>andDictionary<TKey, TValue>to ensure type safety.
9. Best Practices
-
Use constraints to limit generics if necessary. E.g.,
class MathOperation<T> where T : structensures T can only be a value type (like a number), preventing someone from doing math on strings.
10. Exercises
-
1.
Create a
Dictionary<int, string>to map employee IDs (Key) to employee names (Value). Add 3 employees and print one out by looking up their ID.
-
2.
Create a generic class
Storage<T>that holds an array of 3 items of typeT.
11. MCQs with Answers
Question 1
What does the <T> syntax signify in C#?
Question 2
What is the primary benefit of Generics?
Question 3
Which collection stores data in Key-Value pairs?
Question 4
What happens if you try to retrieve a key from a Dictionary that does not exist?
Question 5
A Queue<T> operates on which principle?
Question 6
Which method adds an item to a Queue?
Question 7
A Stack<T> operates on which principle?
Question 8
Which method removes and returns the top item from a Stack?
Question 9
Which generic collection is the modern, type-safe replacement for the old Hashtable?
where T : class constraint b) No, generics must accept all types
Answer: a) Yes, using the where T : class constraint
12. Interview Questions
-
Q: What is the difference between
List<T>andDictionary<TKey, TValue>? When would you use one over the other?
-
Q: Explain the difference between
QueueandStack. Provide a real-world use case for each in software development (e.g., Undo functionality vs. Task scheduling).
-
Q: Why are Generics more performant than using
objectreferences (Boxing/Unboxing)?
13. Summary
Generics<T> empower you to write flexible, reusable code that works across multiple data types while maintaining strict type safety. Combining this with powerful structures like Dictionary, Queue, and Stack allows you to solve complex data management problems efficiently.