1Working with C# Collections
Hi friends, in this article we will learn collections in C#. Collections store and manage groups of objects. Unlike arrays, many collections can grow or shrink dynamically. C# provides different collection types for different requirements. We will explore List, Dictionary, HashSet and Queue. These are commonly used in APIs, shopping applications, banking systems and enterprise software.
2Think About a Shopping Application
Imagine an online shopping application. You maintain a list of products, look up product prices using product IDs, avoid duplicate coupon codes and manage customer orders in the order they arrive.
These needs match four collections: List, Dictionary, HashSet and Queue. Choosing the right collection makes your application easier to maintain.
3Four Collections You Should Know
List
A List stores items in order and allows duplicates. You can access items using an index and add or remove items dynamically.
Dictionary
A Dictionary stores key-value pairs. Each key must be unique. It is useful for looking up a value using a key.
HashSet
A HashSet stores unique items. Adding an existing item does not create a duplicate.
Queue
A Queue follows FIFO, meaning First In, First Out. The first item added is the first item removed.
| Collection | Best use |
|---|---|
| List | Ordered items |
| Dictionary | Key-based lookup |
| HashSet | Unique values |
| Queue | First-in processing |
4Build a Collection Demo
Let's create all four collections and perform common operations.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var products = new List<string>
{ "Laptop", "Mouse" };
products.Add("Keyboard");
Console.WriteLine(products.Count);
var prices = new Dictionary<int, string>
{
{ 101, "Laptop" },
{ 102, "Mouse" }
};
Console.WriteLine(prices[101]);
var codes = new HashSet<string>();
codes.Add("SAVE10");
codes.Add("SAVE10");
Console.WriteLine(codes.Count);
var orders = new Queue<string>();
orders.Enqueue("Order A");
orders.Enqueue("Order B");
Console.WriteLine(orders.Dequeue());
}
}
We use Add to insert List, Dictionary and HashSet items. Queue uses Enqueue to add and Dequeue to remove the next item.
5Check the Collection Results
3 Laptop 1 Order A
The List contains three products. Dictionary finds Laptop using key 101. HashSet keeps only one coupon. Queue removes Order A first.
6Watch Each Collection Work
Follow the operations as items are added, looked up, deduplicated and removed from the queue.
7Choose Collections with Confidence
Each collection solves a different problem. List maintains ordered items, Dictionary provides key-based lookup, HashSet prevents duplicates and Queue processes items in arrival order.
- Use List for dynamic ordered data.
- Use Dictionary for unique keys and fast lookups.
- Use HashSet for unique values and Queue for FIFO processing.
8Questions Interviewers May Ask
What is the difference between an array and List?
Answer: An array has a fixed length after creation. A List can dynamically grow or shrink as items are added or removed.
Can a Dictionary contain duplicate keys?
Answer: No. Keys must be unique. Adding an existing key with Add throws an ArgumentException.
What is the difference between HashSet and List?
Answer: List allows duplicate items and supports index access. HashSet stores unique items and does not provide index-based access.
What is FIFO in Queue?
Answer: FIFO means First In, First Out. The first item added is the first item removed using Dequeue.
How do you safely check whether a Dictionary key exists?
Answer: Use TryGetValue. It checks the key and returns the value when found, avoiding a separate lookup and exception for a missing key.