Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Introduction to Dictionaries | Dictionaries and Key-Value Collections
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Lists & Collections

bookIntroduction to Dictionaries

A dictionary in C# is a collection designed to store data as pairs of keys and values. Each key in a dictionary must be unique, meaning you cannot have two entries with the same key. This makes dictionaries ideal for situations where you need to quickly look up a value using a unique identifier, such as a user's ID or a product code.

In C#, a dictionary is represented by the generic type Dictionary<TKey, TValue>, where TKey is the type of the key and TValue is the type of the value. You must specify both types when creating a dictionary. The basic syntax to declare and initialize a dictionary is:

Dictionary<string, int> ages = new Dictionary<string, int>();

Here, string is the type of the key (for example, a person's name), and int is the type of the value (for example, their age). Remember, keys must be unique within the dictionary, but values can be duplicated.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Create a dictionary with country codes as keys and country names as values Dictionary<string, string> countryCodes = new Dictionary<string, string>(); // Add entries to the dictionary countryCodes.Add("US", "United States"); countryCodes.Add("CA", "Canada"); countryCodes.Add("MX", "Mexico"); // Retrieve and display a value using its key string code = "CA"; if (countryCodes.ContainsKey(code)) { Console.WriteLine("The country with code " + code + " is " + countryCodes[code]); } else { Console.WriteLine("Country code not found."); } } } }

To work effectively with dictionaries, you need to know how to add, access, and update entries. When you want to add a new key-value pair, use the Add method, specifying both the key and the value. Accessing a value is straightforward: use the key inside square brackets, like dictionary[key]. If the key exists, you get the corresponding value; if not, you get an error—so it is a good idea to check if the key exists first with ContainsKey.

If you want to update the value for an existing key, simply assign a new value using the key: dictionary[key] = newValue;. This replaces the old value for that key. If the key does not exist, this syntax will add a new entry.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { Dictionary<string, string> countryCodes = new Dictionary<string, string>(); countryCodes.Add("US", "United States"); countryCodes.Add("CA", "Canada"); countryCodes.Add("MX", "Mexico"); // Check if a key exists before accessing or removing string keyToCheck = "MX"; if (countryCodes.ContainsKey(keyToCheck)) { Console.WriteLine("Found: " + keyToCheck + " - " + countryCodes[keyToCheck]); } // Remove a key-value pair countryCodes.Remove("MX"); // Try to access the removed key if (!countryCodes.ContainsKey("MX")) { Console.WriteLine("Key 'MX' has been removed from the dictionary."); } } } }

1. What is the main feature of a dictionary compared to a list?

2. Which method checks if a key exists in a dictionary?

3. Fill in the blanks to add a new key-value pair to a Dictionary<string, int> named ages with key "Alice" and value 30:

question mark

What is the main feature of a dictionary compared to a list?

Select the correct answer

question mark

Which method checks if a key exists in a dictionary?

Select the correct answer

question-icon

Fill in the blanks to add a new key-value pair to a Dictionary<string, int> named ages with key "Alice" and value 30:

("Alice", );
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookIntroduction to Dictionaries

Stryg for at vise menuen

A dictionary in C# is a collection designed to store data as pairs of keys and values. Each key in a dictionary must be unique, meaning you cannot have two entries with the same key. This makes dictionaries ideal for situations where you need to quickly look up a value using a unique identifier, such as a user's ID or a product code.

In C#, a dictionary is represented by the generic type Dictionary<TKey, TValue>, where TKey is the type of the key and TValue is the type of the value. You must specify both types when creating a dictionary. The basic syntax to declare and initialize a dictionary is:

Dictionary<string, int> ages = new Dictionary<string, int>();

Here, string is the type of the key (for example, a person's name), and int is the type of the value (for example, their age). Remember, keys must be unique within the dictionary, but values can be duplicated.

Program.cs

Program.cs

copy
12345678910111213141516171819202122232425262728293031
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { // Create a dictionary with country codes as keys and country names as values Dictionary<string, string> countryCodes = new Dictionary<string, string>(); // Add entries to the dictionary countryCodes.Add("US", "United States"); countryCodes.Add("CA", "Canada"); countryCodes.Add("MX", "Mexico"); // Retrieve and display a value using its key string code = "CA"; if (countryCodes.ContainsKey(code)) { Console.WriteLine("The country with code " + code + " is " + countryCodes[code]); } else { Console.WriteLine("Country code not found."); } } } }

To work effectively with dictionaries, you need to know how to add, access, and update entries. When you want to add a new key-value pair, use the Add method, specifying both the key and the value. Accessing a value is straightforward: use the key inside square brackets, like dictionary[key]. If the key exists, you get the corresponding value; if not, you get an error—so it is a good idea to check if the key exists first with ContainsKey.

If you want to update the value for an existing key, simply assign a new value using the key: dictionary[key] = newValue;. This replaces the old value for that key. If the key does not exist, this syntax will add a new entry.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930313233
using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { Dictionary<string, string> countryCodes = new Dictionary<string, string>(); countryCodes.Add("US", "United States"); countryCodes.Add("CA", "Canada"); countryCodes.Add("MX", "Mexico"); // Check if a key exists before accessing or removing string keyToCheck = "MX"; if (countryCodes.ContainsKey(keyToCheck)) { Console.WriteLine("Found: " + keyToCheck + " - " + countryCodes[keyToCheck]); } // Remove a key-value pair countryCodes.Remove("MX"); // Try to access the removed key if (!countryCodes.ContainsKey("MX")) { Console.WriteLine("Key 'MX' has been removed from the dictionary."); } } } }

1. What is the main feature of a dictionary compared to a list?

2. Which method checks if a key exists in a dictionary?

3. Fill in the blanks to add a new key-value pair to a Dictionary<string, int> named ages with key "Alice" and value 30:

question mark

What is the main feature of a dictionary compared to a list?

Select the correct answer

question mark

Which method checks if a key exists in a dictionary?

Select the correct answer

question-icon

Fill in the blanks to add a new key-value pair to a Dictionary<string, int> named ages with key "Alice" and value 30:

("Alice", );
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 1
some-alt