Working with collections is one of the most essential skills for any .NET Core developer. Whether you're building web applications, APIs, enterprise systems, or microservices, collections help you store, manage, and manipulate data.
This article covers:
1. Types of Collections
2.Generic & Non-Generic Collections
3.Syntax + Examples
4. Real-time use cases
5. Performance considerations
1. What Are Collections in .NET Core?
A Collection is a group of objects stored together in memory.
Collections help developers:
Store multiple values in a single structure
Efficiently add, update, delete, and search items
Work with dynamic data (unlike arrays)
Collections exist in two major forms:
1. Non-Generic Collections — System.Collections
2. Generic Collections — System.Collections.Generic
Let’s explore them in detail.
2. Non-Generic Collections (Old Model)
Namespace: System.Collections
These collections store objects , not typed items, meaning values are stored as object type → boxing & unboxing happen → less performance.
Non-generic collections still appear in interviews, but not used much in modern .NET Core.
2.1 ArrayList
Stores a dynamic array of mixed data types.
Syntax
ArrayList list = new ArrayList();
list.Add(10);
list.Add("Hello");
list.Add(12.5);
Real-Time Example
Used in legacy systems where type safety was not enforced.
Example: Reading rows from Excel where data types are not known beforehand.
2.2 Hashtable
Stores key-value pairs with unique keys .
Keys & values are stored as objects .
Syntax
Hashtable ht = new Hashtable();
ht["username"] = "Admin123";
ht["loginCount"] = 5;
Real-Time Example
Used in old authentication systems to store configuration settings.
Example: ApplicationSettings["Theme"] = "Dark"
Drawback of Non-Generic Collections
3. Generic Collections (Modern & Recommended)
Namespace: System.Collections.Generic
Advantages:
Let’s explore the important ones.
3.1 List — Most Common Collection
Dynamic, strongly-typed list.
Syntax
List<string> names = new List<string>();
names.Add("John");
names.Add("Priya");
names.Add("Rahul");
Real-Time Example (API Development)
Fetching a list of users from database:
public List<UserModel> GetUsers()
{
return _context.Users.ToList();
}
Used everywhere in:
1. Pagination
2. Search filters
3. API response models
3.2 Dictionary<TKey, TValue> — Key-Value Collection
Fastest lookup collection in .NET.
Syntax
Dictionary<int, string> products = new Dictionary<int, string>();
products.Add(1, "Laptop");
products.Add(2, "Mobile");
Real-Time Example (Shopping Cart)
Dictionary<int, int> cart = new Dictionary<int, int>();
cart[101] = 2; // ProductId=101, Quantity=2
cart[102] = 1;
Used in:
1. Caching
2. Configurations
3. Role access permissions
3.3 Queue — FIFO (First In First Out)
Syntax
Queue<string> tasks = new Queue<string>();
tasks.Enqueue("Send Email");
tasks.Enqueue("Generate Invoice");
tasks.Enqueue("Backup Database");
var nextTask = tasks.Dequeue();
Real-Time Example (Background Jobs)
Used in job scheduling systems like:
1. BackgroundService
2. Azure Queue
3. RabbitMQ
Example: Email sending queue.
3.4 Stack — LIFO (Last In First Out)
Syntax
Stack<string> history = new Stack<string>();
history.Push("Home");
history.Push("Products");
history.Push("Cart");
var lastVisited = history.Pop();
Real-Time Example
1. Browser back button
2.Undo operations in editor
3.5 HashSet — Unique Values Collection
Syntax
HashSet<int> numbers = new HashSet<int>() { 1, 2, 3, 3, 4 };
Result: 1,2,3,4 (no duplicates)
Real-Time Example
1. Removing duplicate emails
2. Unique visitor IDs
3. Unique tags in blog
3.6 SortedList<TKey,TValue>
Sorted by key automatically.
Example
SortedList<int, string> employees = new SortedList<int, string>();
employees.Add(3, "Anu");
employees.Add(1, "Rahul");
employees.Add(2, "Priya");
Output is always sorted by key.
3.7 Concurrent Collections (Thread-Safe)
Used in microservices & real-time apps.
Examples
ConcurrentDictionary
ConcurrentQueue
BlockingCollection
Real-Time Example (High Traffic APIs)
var cache = new ConcurrentDictionary<string, string>();
cache.TryAdd("JWT", "eygsd79t..");
Used in:
1. Parallel programming
2. Multi-threaded apps
3. Real-time dashboards
4. Real-Time End-to-End Example (Full .NET Core Scenario)
Scenario
An E-commerce application needs to process:
Product list
Shopping cart
User browsing history
Background tasks
Unique coupon codes
Product List (List)
List<Product> products = _db.Products.ToList();
Shopping Cart (Dictionary<TKey, TValue>)
Dictionary<int, int> cart = new Dictionary<int, int>();
cart[productId] = quantity;
Browsing History (Stack)
Stack<string> history = new Stack<string>();
history.Push("Product-1");
history.Push("Product-2");
Order Processing Queue (Queue)
Queue<Order> orderQueue = new Queue<Order>();
orderQueue.Enqueue(order);
Unique Coupon Codes (HashSet)
HashSet<string> coupons = new HashSet<string>();
coupons.Add("OFF50");
5. Collection Summary Table
| Collection | Type | Use Case |
|---|
| List | Linear dynamic list | API responses, user lists |
| Dictionary<TKey,TValue> | Key-value | Config, cache, cart |
| Queue | FIFO | Job scheduling |
| Stack | LIFO | Browser history |
| HashSet | Unique items | Removing duplicates |
| SortedList | Sorted key-value | Ranking, ordered display |
| ConcurrentDictionary | Thread-safe dictionary | Microservices |
Conclusion
Collections are the backbone of .NET Core applications.
Understanding where and how to use each collection helps you:
1.Improve performance
2.Write cleaner code
3.Build scalable applications
4.Perform better in interviews