Difference between revisions of "Dictionaries"
(→Definition) |
|||
(9 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | ==Definition== | + | ==Definition== |
− | A dictionary is a general-purpose data structure for storing a group of objects. A dictionary has a set of keys and each key has a single associated value. When presented with a key, the dictionary will return the associated value. Dictionaries are special lists, whereas every value in the list has a key which is also a variable. A good example for a dictionary is a phone book.< | + | A dictionary is a general-purpose data structure for storing a group of objects. A dictionary has a set of keys and each key has a single associated value. When presented with a key, the dictionary will return the associated value. Dictionaries are special lists, whereas every value in the list has a key which is also a variable. A good example for a dictionary is a phone book. |
− | < | + | |
+ | <youtube>https://www.youtube.com/watch?v=eiWhNptRztk&index=12&list=PLCiOXwirraUD0G290WrVKpVYd3leGRRMW</youtube> | ||
+ | |||
+ | https://www.youtube.com/watch?v=eiWhNptRztk&index=12&list=PLCiOXwirraUD0G290WrVKpVYd3leGRRMW | ||
+ | |||
+ | ===TRC Video=== | ||
+ | <youtube>https://www.youtube.com/watch?v=BJL-JxZDAQc</youtube> | ||
+ | |||
+ | https://www.youtube.com/watch?v=BJL-JxZDAQc | ||
+ | |||
+ | ==Declaring & Adding to the Dictionary == | ||
+ | <syntaxhighlight lang=csharp> | ||
Dictionary<string, long> phonebook = new Dictionary<string, long>(); | Dictionary<string, long> phonebook = new Dictionary<string, long>(); | ||
phonebook.Add("Alex", 4154346543); | phonebook.Add("Alex", 4154346543); | ||
phonebook["Jessica"] = 4159484588; | phonebook["Jessica"] = 4159484588; | ||
− | < | + | </syntaxhighlight> |
Notice that when defining a dictionary, we need to provide a generic definition with two types - the type of the key and the type of the value. In this case, the key is a string whereas the value is an integer. | Notice that when defining a dictionary, we need to provide a generic definition with two types - the type of the key and the type of the value. In this case, the key is a string whereas the value is an integer. | ||
+ | |||
There are also two ways of adding a single value to the dictionary, either using the brackets operator or using the Add method. | There are also two ways of adding a single value to the dictionary, either using the brackets operator or using the Add method. | ||
− | + | ||
+ | ==Checking for a key== | ||
To check whether a dictionary has a certain key in it, we can use the ContainsKey method: | To check whether a dictionary has a certain key in it, we can use the ContainsKey method: | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
Dictionary<string, long> phonebook = new Dictionary<string, long>(); | Dictionary<string, long> phonebook = new Dictionary<string, long>(); | ||
phonebook.Add("Alex", 415434543); | phonebook.Add("Alex", 415434543); | ||
Line 19: | Line 34: | ||
Console.WriteLine("Alex's number is " + phonebook["Alex"]); | Console.WriteLine("Alex's number is " + phonebook["Alex"]); | ||
} | } | ||
− | < | + | </syntaxhighlight> |
− | + | ||
+ | ==Removing a key/value pair== | ||
To remove an item from a dictionary, we can use the Remove method. Removing an item from a dictionary by its key is fast and very efficient. When removing an item from a List using its value, the process is slow and inefficient, unlike the dictionary Remove function. | To remove an item from a dictionary, we can use the Remove method. Removing an item from a dictionary by its key is fast and very efficient. When removing an item from a List using its value, the process is slow and inefficient, unlike the dictionary Remove function. | ||
+ | |||
+ | <syntaxhighlight lang=csharp> | ||
Dictionary<string, long> phonebook = new Dictionary<string, long>(); | Dictionary<string, long> phonebook = new Dictionary<string, long>(); | ||
phonebook.Add("Alex", 415434543); | phonebook.Add("Alex", 415434543); | ||
Line 28: | Line 46: | ||
phonebook.Remove("Jessica"); | phonebook.Remove("Jessica"); | ||
Console.WriteLine(phonebook.Count); | Console.WriteLine(phonebook.Count); | ||
+ | </syntaxhighlight> |
Latest revision as of 08:27, 23 August 2023
Contents
Definition
A dictionary is a general-purpose data structure for storing a group of objects. A dictionary has a set of keys and each key has a single associated value. When presented with a key, the dictionary will return the associated value. Dictionaries are special lists, whereas every value in the list has a key which is also a variable. A good example for a dictionary is a phone book.
https://www.youtube.com/watch?v=eiWhNptRztk&index=12&list=PLCiOXwirraUD0G290WrVKpVYd3leGRRMW
TRC Video
https://www.youtube.com/watch?v=BJL-JxZDAQc
Declaring & Adding to the Dictionary
Dictionary<string, long> phonebook = new Dictionary<string, long>();
phonebook.Add("Alex", 4154346543);
phonebook["Jessica"] = 4159484588;
Notice that when defining a dictionary, we need to provide a generic definition with two types - the type of the key and the type of the value. In this case, the key is a string whereas the value is an integer.
There are also two ways of adding a single value to the dictionary, either using the brackets operator or using the Add method.
Checking for a key
To check whether a dictionary has a certain key in it, we can use the ContainsKey method:
Dictionary<string, long> phonebook = new Dictionary<string, long>();
phonebook.Add("Alex", 415434543);
phonebook["Jessica"] = 415984588;
if (phonebook.ContainsKey("Alex"))
{
Console.WriteLine("Alex's number is " + phonebook["Alex"]);
}
Removing a key/value pair
To remove an item from a dictionary, we can use the Remove method. Removing an item from a dictionary by its key is fast and very efficient. When removing an item from a List using its value, the process is slow and inefficient, unlike the dictionary Remove function.
Dictionary<string, long> phonebook = new Dictionary<string, long>();
phonebook.Add("Alex", 415434543);
phonebook["Jessica"] = 415984588;
phonebook.Remove("Jessica");
Console.WriteLine(phonebook.Count);