0Pricing
C# Academy · Lesson

Working with Collections: Lists, Dictionaries, and Queues

Dive into C# collections to manage dynamic data efficiently.

Working with Collections: Lists, Dictionaries, and Queues is a free C# Academy lesson on CoddyKit — lesson 6 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Working with Collections: Lists, Dictionaries, and Queues

Welcome to the next lesson! In this lesson, you’ll learn about collections in C#, including lists, dictionaries, and queues, and how to use them effectively in your programs. Let’s get started!

Working with Collections: Lists, Dictionaries, and Queues — illustration 1

2

What are Collections?

Collections in C# are classes that store and manage groups of related objects. Unlike arrays, collections are dynamic and offer more flexibility and functionality.

Key Point: Collections make it easier to work with groups of data by providing built-in methods for adding, removing, and searching elements.

3

Working with Lists

A list is a collection that allows duplicate elements and maintains their order. Example:

Tip: Use lists when you need to store and manipulate ordered collections of data.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
        
        names.Add("Diana"); // Add an element
        names.Remove("Bob"); // Remove an element

        Console.WriteLine("Names:");
        foreach (string name in names) {
            Console.WriteLine(name);
        }
    }
}

4

Working with Dictionaries

A dictionary is a collection of key-value pairs. Each key is unique, and it is used to access its associated value. Example:

Key Point: Dictionaries are ideal for scenarios where you need fast lookups based on unique keys.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Dictionary<string, int> ages = new Dictionary<string, int> {
            { "Alice", 25 },
            { "Bob", 30 },
            { "Charlie", 35 }
        };

        ages["Diana"] = 28; // Add a key-value pair
        ages.Remove("Bob"); // Remove a key-value pair

        Console.WriteLine("Ages:");
        foreach (var entry in ages) {
            Console.WriteLine($"{entry.Key}: {entry.Value}");
        }
    }
}

5

Working with Queues

A queue is a collection that follows the First In, First Out (FIFO) principle. Elements are added at the end and removed from the front. Example:

Tip: Use queues for tasks that need to be processed in the order they were added.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> tasks = new Queue<string>();

        tasks.Enqueue("Task 1"); // Add to the queue
        tasks.Enqueue("Task 2");
        tasks.Enqueue("Task 3");

        Console.WriteLine("Processing tasks:");
        while (tasks.Count > 0) {
            Console.WriteLine(tasks.Dequeue()); // Remove from the queue
        }
    }
}

6

Comparing Collections

Here’s a quick comparison of the collections:

  • List: Maintains order, allows duplicates, and provides indexed access.
  • Dictionary: Stores key-value pairs for fast lookups.
  • Queue: Processes elements in a First In, First Out order.

Tip: Choose the collection type that best fits your use case.

7

Practice Challenge

Create a program that uses a dictionary to store product prices and a queue to simulate customer checkout. Example:

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Dictionary<string, double> products = new Dictionary<string, double> {
            { "Apple", 0.5 },
            { "Banana", 0.3 },
            { "Carrot", 0.2 }
        };

        Queue<string> customers = new Queue<string>();
        customers.Enqueue("Customer 1");
        customers.Enqueue("Customer 2");

        Console.WriteLine("Processing customers:");
        while (customers.Count > 0) {
            string customer = customers.Dequeue();
            Console.WriteLine($"{customer} is buying:");
            foreach (var product in products) {
                Console.WriteLine($"- {product.Key}: ${product.Value}");
            }
        }
    }
}

8

9

Best Practices for Collections

Here are some best practices:

  • Choose the right collection type based on your requirements (e.g., fast lookups, order preservation, or FIFO processing).
  • Use generic collections (e.g., List<T>, Dictionary<TKey, TValue>) for type safety.
  • Always check for null or empty collections before accessing elements.

Tip: Properly managing collections can make your code more efficient and readable.

10

Great Job!

Congratulations! You’ve learned how to use lists, dictionaries, and queues to manage data dynamically in C#. This concludes the Advanced Programming Concepts category. Let’s continue coding and explore more advanced topics in your development journey!

Working with Collections: Lists, Dictionaries, and Queues — illustration 10

Frequently asked questions

Is the “Working with Collections: Lists, Dictionaries, and Queues” lesson free?

Yes — the full text of “Working with Collections: Lists, Dictionaries, and Queues” is free to read here on the web, and the C# Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Working with Collections: Lists, Dictionaries, and Queues”?

Dive into C# collections to manage dynamic data efficiently. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 6 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “Working with Collections: Lists, Dictionaries, and Queues” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to Interfaces
  2. Abstract Classes
  3. Static Members and Methods
  4. Exploring Enums and Structs
  5. Delegates and Events
  6. Working with Collections: Lists, Dictionaries, and Queues
← Back to C# Academy