Dictionary
C# also provides us a structure to store data as key/value pairs. C# calls these objects dictionaries, and they are provided by the Dictionary
class.
Considering the gradebook example, we can improve our program using a dictionary. We’ll store the students’ grades along with their names in the same data structure. The names will be the keys, and the grades will be the values.
As with the other collection structures, in C# we must specify the types of the objects we’ll be storing when we declare a variable or parameter to be a dictionary. This means specifying both key and value data types, which are allowed to be different types for a given dictionary.
You can find the DictionaryGradebook
project in the csharp-web-dev-examples
repository. Explore it, run it, and notice how it differs from the ListGradebook
and the ArrayGradebook
.
|
|
Notice how a Dictionary
called students is declared on line 2:
|
|
Here, <string, double>
defines the data types for this dictionary’s <key, value>
pairs.
We can add a new item with a .Add()
method, specifying both key and value:
|
|
And while we don’t do so in this example, we may also access Dictionary
elements using bracket notation. If we had a key/value pair of "jesse"/4.0
in the students
dictionary, we could access the grade with:
double jesseGrade = students["jesse"];
Variables may be used to access elements:
string name = "jesse";
double jesseGrade = students[name];
Looping through a dictionary is slightly more complex than it is for ordered lists. Let’s look at the foreach
loop from this example:
|
|
The iterator variable, student, is of type KeyValuePair<string, double>
. The class KeyValuePair<T,T> is specifically constructed to be used in this fashion, to represent key/value pairs within dictionaries. Each KeyValuePair
object has a Key
property and a Value
property.
If you only need to access the key of each item, you can construct a simpler loop and use the Keys property of the Dictionary
class:
foreach (string studentName in students.Keys) {
Console.WriteLine(studentName);
}
A similar structure applies if you only need the values, using students.Values
:
foreach (double grade in students.Values) {
Console.WriteLine(grade);
}
We can declare and initialize a dictionary in one stroke like so:
Dictionary<int, string> groceries = new Dictionary<int, string>
{
{2, "Apples"},
{3, "Oranges"},
{1, "Avocado"}
};
Dictionary Methods
Let’s collect some Dictionary
methods as we have for List
. As we said about Lists
, this is by no means a comprehensive catalog. For full details on all properties and methods available, see the documentation
on the Dictionary
class.
For the purposes of this table, we’ll create a dictionary to hold our solar system’s planets and the number of moons associated with each.
Dictionary<string, int> moons = new Dictionary<string, int>();
moons.Add("Mercury", 0);
moons.Add("Venus", 0);
moons.Add("Earth", 1);
moons.Add("Mars", 2);
moons.Add("Jupiter", 79);
moons.Add("Saturn", 82);
moons.Add("Uranus", 27);
moons.Add("Neptune", 14);
Dictionary Methods and Properties
We have only brushed the surface of how arrays, Lists
, and dictionaries work. We leave it to you to refer to the official documentation linked below for more details. You’ll certainly be using Lists
and dictionaries in more ways than those covered in this lesson, but with the knowledge you have now, you should be able to use C# collections and learn new uses as you go.
Check Your Understanding
Given our Dictionary
,
|
|
What is the syntax to get the key names?
Dictionary.Keys(moons);
moons.Keys();
moons.Keys;
moons.KeySet();
Given our Dictionary
,
|
|
What will moons[“Mars”]; return?
2
{Mars: 2}
2.0
"Mars"