Curriculum
Collections and Generics in C# are among the most important concepts for professional software development. Collections and Generics in C# help developers efficiently store, manage, organize, and manipulate groups of data. Almost every ASP.NET Core Application, Web API, Desktop Application, Cloud Solution, Mobile App, and Enterprise Software System uses Collections and Generics in C# extensively.
Understanding Collections and Generics in C# is essential because real-world applications rarely work with a single value. Instead, applications manage thousands or even millions of records such as users, products, orders, students, employees, transactions, and logs.
A Collection is a data structure used to store multiple objects together.
Instead of:
string student1 = "Rahul";
string student2 = "Amit";
string student3 = "Priya";
We can use a collection:
string[] students =
{
"Rahul",
"Amit",
"Priya"
};
Collections make data management easier and more scalable.
Collections and Generics in C# help developers:
Without collections, modern software development would be extremely difficult.
Collections are broadly divided into:
Examples:
ArrayList
Hashtable
Queue
Stack
Examples:
List<T>
Dictionary<TKey,TValue>
Queue<T>
Stack<T>
HashSet<T>
Generic collections are preferred in modern .NET development.
Generics allow developers to create type-safe classes, methods, and collections.
Example:
List<string> students =
new List<string>();
Only string values can be stored.
Benefits:
Generics are heavily used in professional applications.
Arrays are the simplest collection type.
Example:
int[] numbers =
{
10,
20,
30,
40
};
Accessing Elements:
Console.WriteLine(
numbers[0]);
Output:
10
Arrays are fixed in size after creation.
Arrays:
Example:
int[] numbers =
new int[3];
Only three elements can be stored.
For dynamic data, other collections are better.
ArrayList is a non-generic collection.
Example:
ArrayList list =
new ArrayList();
list.Add(100);
list.Add("Rahul");
list.Add(true);
Output:
100
Rahul
True
ArrayList can store different data types.
Example:
ArrayList list =
new ArrayList();
list.Add("Rahul");
int age =
(int)list[0];
Output:
Runtime Error
ArrayList lacks type safety.
For this reason, generic collections are preferred.
List<T> is the most commonly used generic collection.
Example:
List<string> students =
new List<string>();
Adding Items:
students.Add("Rahul");
students.Add("Amit");
students.Add("Priya");
Displaying Items:
foreach(string student
in students)
{
Console.WriteLine(
student);
}
Output:
Rahul
Amit
Priya
List<T> automatically resizes as data grows.
students.Add("Rohan");
students.Remove("Amit");
students.Count
students.Contains("Rahul");
These operations are used daily in software development.
Dictionary stores data as:
Key → Value
Example:
Dictionary<int,string>
students =
new Dictionary<int,string>();
Adding Data:
students.Add(101,"Rahul");
students.Add(102,"Amit");
Accessing Data:
Console.WriteLine(
students[101]);
Output:
Rahul
Dictionary provides very fast lookups.
Example:
Student ID → Student Name
Product ID → Product Name
Employee ID → Employee Name
These mappings are common in enterprise systems.
Queue follows:
FIFO
First In First Out
Example:
Queue<string> queue =
new Queue<string>();
queue.Enqueue("A");
queue.Enqueue("B");
queue.Enqueue("C");
Removing:
queue.Dequeue();
Output:
A
The first item added is removed first.
Examples:
Ticket Booking
Call Center Systems
Hospital Waiting Lists
Print Queues
Queue structures are widely used.
Stack follows:
LIFO
Last In First Out
Example:
Stack<string> stack =
new Stack<string>();
stack.Push("A");
stack.Push("B");
stack.Push("C");
Removing:
stack.Pop();
Output:
C
The last item added is removed first.
Examples:
Undo Operations
Browser History
Function Calls
Expression Evaluation
Stack is extremely important in computer science.
HashSet stores unique values.
Example:
HashSet<int> numbers =
new HashSet<int>();
numbers.Add(10);
numbers.Add(20);
numbers.Add(10);
Output:
10
20
Duplicate values are ignored.
Examples:
Unique User IDs
Unique Email Addresses
Unique Product Codes
HashSet is useful when uniqueness is required.
| Generic Collections | Non-Generic Collections |
|---|---|
| Type Safe | Not Type Safe |
| Better Performance | Slower |
| Compile-Time Checking | Runtime Errors Possible |
| Modern Approach | Legacy Approach |
| Preferred in .NET | Rarely Used Today |
Generic collections should be preferred whenever possible.
Example:
List<int> numbers =
new List<int>();
No boxing and unboxing are required.
Non-generic collections often require:
Boxing
Unboxing
which reduces performance.
Customers
Accounts
Transactions
Loans
Products
Orders
Categories
Customers
Patients
Doctors
Appointments
Prescriptions
Students
Teachers
Courses
Attendance Records
Collections are used everywhere in software development.
Prefer:
List<string>
instead of:
ArrayList
Use:
Minimize boxing and unboxing.
Example:
List<Student>
instead of:
List<object>
Prefer generic collections.
Understand the purpose of each collection.
Use strongly typed collections.
Lists are usually a better choice.
Collections are data structures used to store and manage groups of objects.
Generics provide type-safe classes, methods, and collections.
List<T> provides type safety and better performance.
Queue uses FIFO while Stack uses LIFO.
Dictionary stores data as key-value pairs.
HashSet stores unique values without duplicates.
Collections are data structures used to store and manage multiple objects.
Generics allow type-safe programming and improve performance.
It is dynamic, type-safe, and easy to manage.
ArrayList is non-generic, while List<T> provides type safety and better performance.
Dictionary<TKey,TValue> should be used.
They are fundamental for storing, organizing, and processing business data efficiently.
WhatsApp us