Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Dictionary Keys and Values6:06 with Jeremy McLain
Dictionaries contain both keys and values.
Now that we can translate from text to Morse code we 0:00 may also want to do the reverse. 0:03 Given a Morse code message we can translate it back to text for 0:06 people to read. 0:09 To do that, 0:10 we'll want to create a dictionary that translate from Morse code to characters. 0:11 Dictionaries are only fast for finding keys. 0:16 Finding values is another story. 0:18 In fact finding the key for a value can be much slower than searching an array. 0:21 Here dictionaries can save the day again. 0:26 What we need is another dictionary where the keys are the Morse Code and 0:28 the values are the characters. 0:32 We can create this dictionary when the Morse code translator class is created. 0:34 First, let's create a field that's initialized to an empty dictionary. 0:38 So back in MorseCodeTranslator.cs, 0:43 right here let's create another private static dictionary. 0:45 Only this time the key will be string and the value will be a character. 0:52 We'll call it morseToText. 0:59 And we'll go ahead and initialize it right here. 1:02 So say new dictionary string, char. 1:05 There you go. 1:11 So we could go up here and copy all of these key value pairs and paste 1:13 them down here and then swap the keys with the values, but there's a better way. 1:18 To fill the Morse to text dictionary, we'll need to write a static constructor. 1:24 A static constructor is called before any static members of the class are used. 1:29 So before the Morse to text dictionary is used, 1:33 this static constructor will be called. 1:36 The static constructor of a class is run once and only once. 1:38 This is different than the instance constructors that are called 1:42 to instantiate an instance of the class. 1:45 We want the Morse to text and text to Morse dictionaries to be the same for 1:47 all instances of the class so we've made them static. 1:51 Static constructors can only access static members of the class. 1:54 So here I'll say static MorseCodeTranslator. 2:00 Don't forget the opening and closing parentheses. 2:08 Also notice that static constructors don't have a public or private access modifier 2:12 that's because they can actually never be called from code. 2:16 Inside the static constructor we'll loop through all of the key value pairs in 2:20 the text to Morse dictionary and then add them into the Morse the text dictionary. 2:24 When looping through a dictionary using the 4H method 2:28 we can loop through just the keys, just the values or the key value pairs. 2:31 When looping through the key value pairs there's a special type that holds 2:36 both the key and the value, it's called key value pair. 2:39 So say for each key value pair and this is also a generic type. 2:43 And so its types match the dictionary that's being looped through. 2:52 So we'll say char, string and we call this key value pair the code. 2:57 So for each key value pair in textToMorse. 3:03 So this is for each key value pair in textToMorse. 3:11 Key value pair objects have properties for key and the value. 3:16 Let's add the mapping from Morse code to a character here. 3:20 So will say morseToText. 3:23 And in here we're gonna pass in code.value because that's the key 3:28 in the morseToText dictionary and we'll assign it the code.key. 3:33 So this is how we set the value for a key. 3:38 If the key doesn't already exist in the dictionary 3:40 then it's added in the values assigned to it. 3:43 If we were to add a value to the dictionary using a key that already 3:46 existed, then the previous value would just be overridden with the new value. 3:49 Another way to add items to a dictionary is with the add method. 3:54 However, it behaves a little differently. 3:58 The add method can only be used to add new keys to the dictionary. 4:00 If we call the add method and 4:05 pass in a key that already exists an exception will be thrown. 4:06 So to change this to using the in the add method we'll just say morseToText.Add and 4:10 the add method in a dictionary actually takes two parameters. 4:16 The first is the key, so that's code.Value in this case and 4:21 the second is the value which is code.Key in this case. 4:24 Confused yet? 4:28 Since we know that there shouldn't be any duplicate keys, we'll go ahead and 4:30 use this one in our foreach loop. 4:33 Let's take a quick look at the documentation for 4:35 the dictionary collection to see what other properties and methods it offers. 4:38 So down here we"ll click on dictionary. 4:42 Because we're dealing with both keys and 4:45 values, you'll notice that many of these methods look a little different. 4:47 The Keys property returns a collection of all of the keys in the dictionary. 4:51 Likewise the Values property returns a collection of all the values. 4:55 Sometimes we only need to loop through the Keys or the Values of a dictionary. 4:59 Here's the Add method we just used. 5:03 The Remove method just takes the key. 5:05 Just like with hash set, Removes are very fast in dictionaries because internally 5:09 the keys are stored in a set. 5:13 Instead of just one contains method, we have ContainsKey and ContainsValue. 5:15 The ContainsKey method is very fast. 5:21 Values aren't hashed in a dictionary. 5:23 So the ContainsValue method has to search through each value individually 5:25 in order to determine if it's in the dictionary. 5:29 Another helpful method is the TryGetValue method. 5:32 This method can be used to attempt to get a value for a given key. 5:36 Instead of throwing an exception if the key isn't in the dictionary, 5:40 it returns true if the key could be found and false otherwise. 5:44 Now we can use the Morse to text dictionary 5:47 to look up the character that a Morse Code maps to. 5:50 We still need to code up a method that uses this dictionary to translate a whole 5:53 Morse Code message into textual output. 5:57 I'll leave that as an exercise for you. 6:00 I've included an implementation of this method in the downloads for this course. 6:02
You need to sign up for Treehouse in order to download course files.Sign up