What are Dictionaries?
In Arrays, we access data through indexing (arrayName[index]
). In an Array, every value (element) has a unique index, which is used for accessing that value, therefore we can say that an Array has an index-value structure.
There's a similar structure called a Dictionary, in which we have key-value pairs instead. While an index is always an integer number, a key can be of any basic data type, however it's commonly a string
.
The following illustration shows an example illustration of dictionary which stores the number of different fruits:
1. Creating a Dictionary
We can declare a dictionary using the following syntax:
index.cs
1IDictionary<keyDataType, valueDataType> dictionaryName = new Dictionary<keyDataType, valueDataType>();
Here keyDataType
represents the data type of the key while the valueDataType
represents the data type of the values. dictionaryName
is the name of the dictionary.
An implicit declaration is also valid:
index.cs
1var dictionaryName = new Dictionary<keyDataType, valueDataType>();
2. Adding Data
We can use the Add
method to add items to the dictionary:
index.cs
1dictionaryName.Add(keyName, value);
3. Accessing Data
We can access the data in dictionaries using the keys:
index.cs
1dictionaryName[keyName]
Following is an example which demonstrates all three:
index.cs
12345678910111213141516171819202122using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { var student = new Dictionary<string, string>(); student.Add("name", "Noah"); student.Add("country", "Netherlands"); student.Add("subject", "Computer Science"); Console.WriteLine(student["name"]); Console.WriteLine(student["country"]); Console.WriteLine(student["subject"]); } } }
In Dictionaries, Count
attribute shows the number of key-value pairs stored in it. Remove
method takes in a key and removes that key-value pair from the dictionary. Clear
method simply removes all key-value pairs from a dictionary. It will be a good code reading exercise to read and understand the usage of Count
, Remove
and Clear
from the following code:
index.cs
1234567891011121314151617181920212223242526using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { var numbers = new Dictionary<int, string>(); numbers.Add(0, "Zero"); numbers.Add(1, "One"); numbers.Add(2, "Two"); numbers.Add(3, "Three"); numbers.Add(4, "Four"); numbers.Add(5, "Five"); Console.WriteLine(numbers.Count); // Output: 6 numbers.Remove(3); Console.WriteLine(numbers.Count); // Output: 5 numbers.Clear(); Console.WriteLine(numbers.Count); // Output: 0 } } }
1. What module must be imported for using dictionaries?
2. What is the correct syntax for creating a dictionary?
3. What will be the output of the following code?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 2.04
What are Dictionaries?
Deslize para mostrar o menu
In Arrays, we access data through indexing (arrayName[index]
). In an Array, every value (element) has a unique index, which is used for accessing that value, therefore we can say that an Array has an index-value structure.
There's a similar structure called a Dictionary, in which we have key-value pairs instead. While an index is always an integer number, a key can be of any basic data type, however it's commonly a string
.
The following illustration shows an example illustration of dictionary which stores the number of different fruits:
1. Creating a Dictionary
We can declare a dictionary using the following syntax:
index.cs
1IDictionary<keyDataType, valueDataType> dictionaryName = new Dictionary<keyDataType, valueDataType>();
Here keyDataType
represents the data type of the key while the valueDataType
represents the data type of the values. dictionaryName
is the name of the dictionary.
An implicit declaration is also valid:
index.cs
1var dictionaryName = new Dictionary<keyDataType, valueDataType>();
2. Adding Data
We can use the Add
method to add items to the dictionary:
index.cs
1dictionaryName.Add(keyName, value);
3. Accessing Data
We can access the data in dictionaries using the keys:
index.cs
1dictionaryName[keyName]
Following is an example which demonstrates all three:
index.cs
12345678910111213141516171819202122using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { var student = new Dictionary<string, string>(); student.Add("name", "Noah"); student.Add("country", "Netherlands"); student.Add("subject", "Computer Science"); Console.WriteLine(student["name"]); Console.WriteLine(student["country"]); Console.WriteLine(student["subject"]); } } }
In Dictionaries, Count
attribute shows the number of key-value pairs stored in it. Remove
method takes in a key and removes that key-value pair from the dictionary. Clear
method simply removes all key-value pairs from a dictionary. It will be a good code reading exercise to read and understand the usage of Count
, Remove
and Clear
from the following code:
index.cs
1234567891011121314151617181920212223242526using System; using System.Collections.Generic; namespace ConsoleApp { class Program { static void Main(string[] args) { var numbers = new Dictionary<int, string>(); numbers.Add(0, "Zero"); numbers.Add(1, "One"); numbers.Add(2, "Two"); numbers.Add(3, "Three"); numbers.Add(4, "Four"); numbers.Add(5, "Five"); Console.WriteLine(numbers.Count); // Output: 6 numbers.Remove(3); Console.WriteLine(numbers.Count); // Output: 5 numbers.Clear(); Console.WriteLine(numbers.Count); // Output: 0 } } }
1. What module must be imported for using dictionaries?
2. What is the correct syntax for creating a dictionary?
3. What will be the output of the following code?
Obrigado pelo seu feedback!