LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Collections in C# – List, Dictionary, HashSet and Queue

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.

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.

Remember Choose a collection based on how you need to store, find and process data.

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
Pro Tip Dictionary is useful when you frequently need to find data using a unique key.
Common Mistake Dictionary keys must be unique. Use HashSet when your main requirement is preventing duplicates.

4Build a Collection Demo

Let's create all four collections and perform common operations.

Program.csC#
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

Output
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.

Program.cs - Visual Studio Code
▤⌕⑂▦
● Program.cs
 
PROBLEMS  OUTPUT  DEBUG CONSOLE  TERMINAL
 
ReadyUTF-8  CRLF  C#

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

Q1Easy

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.

Q2Easy

Can a Dictionary contain duplicate keys?

Answer: No. Keys must be unique. Adding an existing key with Add throws an ArgumentException.

Q3Medium

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.

Q4Medium

What is FIFO in Queue?

Answer: FIFO means First In, First Out. The first item added is the first item removed using Dequeue.

Q5Hard

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.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.