In this lecture, we'll get practice using a data structure called dictionaries. We'll actually look at several dictionaries in the feed the teddies implementation. But in this lecture will actually create and use a dictionary from scratch. So how do dictionaries work? We associate a value of some data type with a key of some data type. In other words, the dictionary stores key value pairs, and this should remind you of player props where we used a key to store a particular value. In this lecture, we'll learn how to retrieve a value using a key and we'll also learn how to add a value to associate it with a particular key. To start with, I've opened up the Microsoft documentation because we're going to need to use the documentation for our dictionary class to figure out how to use it. So in the search bar, I'll type dictionary. And the very first hit is the one that we want. So the dictionary is a class that represents a collection of keys and values. As you can see, it's a generic, so we provide the data type for the key and the data type for the value, when we create our dictionary object. We'll look at the documentation for constructors and it's overloaded with a lot of possible constructors. But we're just going to use the simplest constructor where we don't provide an argument at all, when we create our dictionary. This is the simple console app that will use to demonstrate how we use a dictionary, and we'll do three things. We'll create a dictionary of high scores by gamertag, and then we'll prompt for and get a gamertag from the user. And then we'll print the high score for that gamertag, or an error message if we don't have a high score for that gamertag. We just looked at the documentation for the constructor for dictionaries, so let's go ahead and create our dictionary. And as you can see IntelliSense isn't helping us at all here, because we can see that the dictionary class is in the system collections, generic name space, and I don't have a using directive for that name space yet. So using system collections generic, And now I'll get some intelligence help as I try to create my dictionary. The key for our dictionary will be the gamertag, and an appropriate data type for the gamertag is a string, and the value in our dictionary will be the high score and that should be an int. So that's our data type. We've bound the key data type and the value data type to particular data types in this generic as we started declaring our variable. I'm not going to call it key value pairs, I'm going to call it high scores. But then I'm just going to tap away to call the constructor with no arguments using the typical new keyword to call a constructor. So now I have a dictionary object but I don't have any high scores in it. The next thing we need to do is figure out how do we add high scores to our dictionary. In the documentation, we should look at the methods that are available for a dictionary, and look the very first one is an add method. And if we look at this we can see its public which is great, it returns void which makes sense. And we have to provide two arguments when we call it. We need to provide the key of the element we're trying to add, and we need to add the value of the element we're trying to add. We won't be adding any Knowles for reference types here, we'll just be adding actual values. So when we call add, we provide the key in the value that we're trying to add to the dictionary. Let's add a high score for somebody named Joe, and Joe got 100 for the high score. And I can compile and run this, and nothing exciting happens yet. But I do want to show you that if we make a mistake and we try to add a value with the same exact key, watch what happens. We get an exception that says an item with the same key has already been added, so we can't have multiple values in our dictionary with the same key. We'll change this to Bob, Joe's brother who happens to be better at this game. So their score is 9001. And finally will have their less adept brother Joe Bob who in fact has a score of 1. I'll run again to show that it doesn't fail though you can't see that the dictionary has actually been built up. Although I could set a breakpoint and debug, and we can actually look at our high scores dictionary before I execute that line of code. If I hover over a high scores, and we look at the dictionary at this point, you can see we have two entries in the dictionary when with the Joe is a key and a value of 100, and one with Bob is a key and a value of 9001. And if I step over that line of code and we go down here and look at high scores, you can see that we've added Joe Bob with a score of 1 as well. I'll stop debugging and get rid of that break point, but we've confirmed that we've actually filled the dictionary properly. Now we'll prompt for and get a gamertag from the user, And now we're holding a gamertag. The next comment says print high score or error message for gamertag. Let's do the printing the high score part first and then we'll see how we can print an error message if necessary. We'll say high score for, and we'll put the gamertag they provided Is, and now we need to actually access the element in the dictionary that is keyed by gamertag. This will look really familiar to you, it's high scores, and then we use square brackets, and we put the key in between the square brackets. So we're using the same syntax that we used for arrays or lists to access a specific element in the array or list. But instead of providing an index between the square brackets, we provide the key for the element that we're trying to retrieve. I'll go ahead and run, and I'll say how about Joe? And it says the high score for Joe is 100, and we'll test the other ones also. So Bob the high scores 9001, And for Joe Bob the high score is 1. And that's all great. But what happens if we make a typo and try to find out the high score for this gamertag? We get an exception, and the exception we get is the key not found exception, and it tells us the given key that we provided is not present in the dictionary. So this works great if gamertag is actually in the dictionary as a key, but it crashes our program if gamertag isn't. So we need an if statement here. If something Specifically, if the gamertag is in the dictionary, we'll do this. And here's our error message. High score for Gamertag Not found. That's a good error message. But we can't compile at this point because we don't know what to put here as our bullying expression, let's go back to the dictionary documentation. And if we look at these methods, we can see one that's called contains key and that might be what we want to find out if the dictionary contains a particular key. So we'll click on that and it says, it determines whether the dictionary contains the specified key, it returns a bool, and we provide the key that we're looking for. So back in our code, all we have to do is say highScores.ContainsKey and provide gamertag as the key we're looking for. Now, you may still be tempted to say == true here, but that is not necessary. That contains key method returns bool, so this is already a Boolean expression as it is. We'll check the valid ones first, so Joe is still good, Bob is still good, And Joe Bob is still good. And then if we try some gamertag that's not in the dictionary, we get the message that the high score for that gamertag was not found. So this is a way to protect against that key not found exception by checking if the dictionary contains the key we're looking for before we try to access it with this square bracket notation. And those are the key ideas behind using a dictionary. To recap, in this lecture, we learned how dictionary store values associated with the key. We got some practice creating and using a dictionary, and we also learned how to use the contains key method, to determine whether or not a dictionary contains a value associated with a particular key.