Exploring Generic Collections

Tutorial 3 of 5

1. Introduction

In this tutorial, we'll explore the functionality and versatility of Generic Collections in C#. Generics allow you to define type-safe data structures, meaning that you can check for the data type at compile time, preventing common type mismatches.

By the end of this tutorial, you'll learn:
- What Generic Collections are and why they're useful
- How to use List, Dictionary, and other generic collections
- Best practices for working with Generic Collections

Prerequisites: Basic knowledge of C# programming language is required.

2. Step-by-Step Guide

Understanding Generic Collections

Generic collections are classes provided by the .NET Framework to store, retrieve, and manipulate data in a type-safe manner. The most commonly used Generic Collections are List, Dictionary, Queue, and Stack.

Using List

List can be thought of as a dynamic array. Here, T represents the type of elements in the list. Let's look at a simple example:

List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);

In this example, we create a list of integers and add three elements to it.

3. Code Examples

Example 1: Using List

Let's explore how to use the List collection:

List<int> numbers = new List<int> {1, 2, 3, 4, 5}; // Initialize the list with some numbers
numbers.Add(6); // Add an item to the end of the list
numbers.Insert(0, 0); // Insert an item at a specific position

foreach (int number in numbers) // Traverse the list
{
    Console.WriteLine(number); // Output: 0, 1, 2, 3, 4, 5, 6
}

Example 2: Using Dictionary

A Dictionary stores key-value pairs. Let's see it in action:

Dictionary<string, int> ages = new Dictionary<string, int>(); // Create a new dictionary
ages.Add("John", 28); // Add a key-value pair
ages.Add("Jane", 30); // Add another key-value pair

foreach (KeyValuePair<string, int> entry in ages) // Traverse the dictionary
{
    Console.WriteLine($"Name: {entry.Key}, Age: {entry.Value}"); // Output: Name: John, Age: 28 and Name: Jane, Age: 30
}

4. Summary

In this tutorial, we've learned about Generic Collections in C#, why they're useful, and how to use List and Dictionary. Practice using these collections and explore others like Queue and Stack.

For further learning, consider reading Microsoft's official documentation on Generic Collections.

5. Practice Exercises

  1. Exercise 1: Create a List of your favorite movies and print them out.
  2. Exercise 2: Create a Dictionary that stores student names and their grades. Print out each student's name and grade.

Solutions:

  1. Solution 1:
List<string> movies = new List<string> {"Inception", "The Dark Knight", "Interstellar"};
foreach (string movie in movies)
{
    Console.WriteLine(movie);
}
  1. Solution 2:
Dictionary<string, string> grades = new Dictionary<string, string> {{"John", "A"}, {"Jane", "B"}};
foreach (KeyValuePair<string, string> entry in grades)
{
    Console.WriteLine($"Name: {entry.Key}, Grade: {entry.Value}");
}

For further practice, consider creating and manipulating a Stack or Queue.