Using Dictionaries and HashSets

Tutorial 2 of 5

1. Introduction

This tutorial aims to help you understand and implement Dictionaries and HashSets in C#. By the end of this tutorial, you will be able to use these data structures to store and retrieve data in a more efficient manner.

You will learn:

  • What are Dictionaries and HashSets
  • How to create, add and remove items from Dictionaries and HashSets
  • How to access and modify data in Dictionaries and HashSets

Prerequisites:
- Basic knowledge of C#
- Visual Studio or any C# supported IDE installed on your machine

2. Step-by-Step Guide

Dictionaries

A Dictionary in C# is a collection of key-value pairs where each key must be unique. The keys in the dictionary are used to access the values.

To declare a dictionary, use the following syntax:

Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();

Where TKey and TValue are the types of keys and values respectively.

Adding items to the dictionary can be done using the Add method:

dictionary.Add("key1", "value1");

Retrieving a value can be done using the key:

string value = dictionary["key1"]; // value will be "value1"

HashSets

A HashSet in C# is a collection of unique elements. It doesn't maintain any order for storing elements.

To declare a HashSet, use the following syntax:

HashSet<T> hashSet = new HashSet<T>();

Where T is the type of elements in the HashSet.

Adding items to the HashSet can be done using the Add method:

hashSet.Add("value1");

3. Code Examples

Dictionary

// Declare a dictionary
Dictionary<string, int> ages = new Dictionary<string, int>();

// Add items to the dictionary
ages.Add("John", 23);
ages.Add("Emma", 21);

// Access an item in the dictionary
Console.WriteLine(ages["John"]); // Outputs: 23

HashSet

// Declare a HashSet
HashSet<string> names = new HashSet<string>();

// Add items to the HashSet
names.Add("John");
names.Add("Emma");

// Check if an item is in the HashSet
Console.WriteLine(names.Contains("John")); // Outputs: True

4. Summary

In this tutorial, we learned how to use Dictionaries and HashSets in C#. We learned how to add items to these collections, and how to access them.

Next, you could learn about other collections in C#, such as Lists and Stacks. You could also learn how to use LINQ to manipulate these collections.

5. Practice Exercises

  1. Create a dictionary that maps product names to their prices. Add a few products to the dictionary, then print the price of a specific product.

  2. Create a HashSet of student names. Add a few names to the HashSet, then check if a certain name is in the HashSet.

  3. (Advanced) Create a dictionary that maps student names to their grades. Add a few students to the dictionary, then calculate the average grade.

Solutions:

Dictionary<string, double> products = new Dictionary<string, double>();
products.Add("Apple", 0.5);
products.Add("Bread", 1.0);
Console.WriteLine(products["Apple"]); // Outputs: 0.5
HashSet<string> students = new HashSet<string>();
students.Add("John");
students.Add("Emma");
Console.WriteLine(students.Contains("Emma")); // Outputs: True
Dictionary<string, int> grades = new Dictionary<string, int>();
grades.Add("John", 90);
grades.Add("Emma", 85);
double average = grades.Values.Average();
Console.WriteLine(average); // Outputs: 87.5