Curriculum
Collections in C# are specialized classes used to store, manage, and manipulate groups of objects dynamically. Collections in C# provide greater flexibility than arrays because they can grow and shrink automatically as data changes. Every .NET developer uses Collections in C# extensively while building Console Applications, ASP.NET Core Applications, MVC Projects, Web APIs, Desktop Applications, Entity Framework Applications, and Enterprise Software Solutions.
Modern applications frequently work with large volumes of dynamic data such as customers, products, students, employees, orders, transactions, and reports. Collections in C# provide efficient ways to manage such data.
Collections in C# are data structures that store multiple objects in a single container.
Unlike arrays, Collections in C# can automatically adjust their size during program execution.
Example:
List<string> students =
new List<string>();
The collection can grow as new elements are added.
Collections simplify data management and improve application scalability.
Collections in C# help developers:
Most enterprise applications rely heavily on collections.
| Feature | Array | Collection |
|---|---|---|
| Size | Fixed | Dynamic |
| Performance | Fast | Flexible |
| Insert/Delete | Difficult | Easy |
| Data Management | Basic | Advanced |
| Scalability | Limited | High |
Collections in C# are generally preferred for modern application development.
Collections are available through:
using System.Collections.Generic;
This namespace provides many useful collection classes.
Examples include:
These are commonly used in professional .NET applications.
The List collection is the most frequently used collection in C#.
Example:
List<string> students =
new List<string>();
Example:
students.Add("Rahul");
students.Add("Amit");
students.Add("Neha");
Example:
foreach(string student in students)
{
Console.WriteLine(student);
}
Output:
Rahul
Amit
Neha
Lists are widely used because they are simple and flexible.
Example:
students.Remove("Amit");
Updated List:
Rahul
Neha
The Remove method deletes specific items.
Example:
Console.WriteLine(students[0]);
Output:
Rahul
Lists support index-based access similar to arrays.
Example:
Console.WriteLine(students.Count);
Output:
2
Count returns the total number of elements.
The Dictionary collection stores data as key-value pairs.
Example:
Dictionary<int, string> students =
new Dictionary<int, string>();
Example:
students.Add(1, "Rahul");
students.Add(2, "Amit");
students.Add(3, "Neha");
Example:
Console.WriteLine(students[2]);
Output:
Amit
Dictionaries provide fast lookups and are heavily used in enterprise software.
Example:
foreach(var student in students)
{
Console.WriteLine(
student.Key + " " +
student.Value
);
}
Output:
1 Rahul
2 Amit
3 Neha
This allows easy processing of key-value data.
The Queue collection follows the FIFO principle.
FIFO means:
First In First Out
Example:
Queue<string> customers =
new Queue<string>();
Example:
customers.Enqueue("Rahul");
customers.Enqueue("Amit");
customers.Enqueue("Neha");
Example:
customers.Dequeue();
Output:
Rahul Removed
Queues are useful for processing tasks in order.
Examples include:
FIFO processing is common in business applications.
The Stack collection follows the LIFO principle.
LIFO means:
Last In First Out
Example:
Stack<string> books =
new Stack<string>();
Example:
books.Push("Book1");
books.Push("Book2");
books.Push("Book3");
Example:
books.Pop();
Output:
Book3 Removed
The last inserted item is removed first.
Examples include:
Stacks are useful when reverse processing is required.
A HashSet stores unique values only.
Example:
HashSet<string> cities =
new HashSet<string>();
Adding Data:
cities.Add("Jaipur");
cities.Add("Delhi");
cities.Add("Jaipur");
Output:
Jaipur
Delhi
Duplicate values are automatically ignored.
Advantages:
HashSet collections are useful when uniqueness is important.
Most modern C# applications use Generic Collections.
Example:
List<int> numbers =
new List<int>();
Benefits:
Generic Collections are preferred over older collection types.
Adds an item.
students.Add("Rahul");
Removes an item.
students.Remove("Rahul");
Removes all items.
students.Clear();
Checks existence.
students.Contains("Rahul");
These methods simplify collection management.
List<Student> students;
List<Product> products;
List<Transaction> transactions;
Dictionary<int, Patient> patients;
Collections in C# are used extensively in real-world business applications.
Collections in C# are heavily used in:
A strong understanding of Collections in C# helps developers build scalable and efficient applications.
Collections in C# are dynamic data structures used to store and manage groups of objects.
Collections provide dynamic sizing and easier data management.
A List is a flexible collection that automatically adjusts its size.
A Dictionary stores data as key-value pairs for fast retrieval.
Queue follows FIFO processing, while Stack follows LIFO processing.
Collections simplify data management and are widely used in professional .NET applications.
WhatsApp us