C-SHARP - 4.5 Dictionary

Hello everyone and welcome to this new video. In this video, we will discuss the notion of dictionary. A very important concept in programming and to be mastered absolutely. It is less used than lists and tables, however in some cases, dictionaries are essential. Dictionary in English is called dictionary. What will differentiate here is that dictionaries will not work with indexes to target things like arrays or lists. Here, we will talk about keys and values. I will explain to you immediately with an example of fruits and vegetables. Here for example the key will be fruits and the value will be for example a peach. And the key will allow direct searches in dictionaries. So to create a dictionary it is very simple, I will do a var dic = new Dictionary with a capital d. And then you use the tags to specify the type of the key and the type of the value. Here it will be string since it is a fruit and the same for the value but for example if we had a dictionary to list for example a gender or men and women we could have put an int or a boolean to say if c is 1 or true it is the man and 2 or false to say that it is the woman conversely it is the same. And to finish you make parentheses; Once again I put var here for reasons of readability, we could very well have put Dictionary here because the type of our dic c is indeed dictionary of string string. We can add values ​​to our dictionary much like a list. With an Add() function and you see here, we have to pass the key as well as the value. We take our fruit peach example from above. We then type Fruits outside the comma quotes and we put our peach. We have seen the first syntax for adding elements to our dictionary, there is another one. You take the name of your dictionary, then you put square brackets. And you give the key, so we the key c is a string so in quotes and I put vegetables. And for the value I do = a quotes bean for example Ok what can we observe? We know this syntax with the square brackets but usually we use them for what? for indexes. Well here for the dictionary, we don't work by index but by key. So, we give him a key. We said that the key corresponds to vegetables so we give him vegetables. So to recap, so far we have created a dictionary and added values ​​to it, either with the Add function or by retrieving its key. Now to display them how? Well it's very simple, I'm going to do a Console.WriteLine(). And I will tell him I want to obtain the fruit that is in my dictionary, or I specify my dictionary and between the square brackets I put the fruit key. And if I launch my program, I do have my peach fruit displayed. Here we have taken the example of fruit, but imagine a dictionary which contains the contact details of millions of people, and which has been taken as the key to the name of a person. Well, we can easily find this person by giving his key or his name and will immediately obtain his contact details. It is very practical unlike lists, you will see the power of a dictionary just after Before that, I would like to see with you what happens if we put a key that is not in our dictionary, that does not exist, We will put for example clothing. If I run the program, well I get an exception. Basically it informs me that the key is not present in the dictionary. And how can I handle this error? There is a method that checks whether or not a key is present in a dictionary. And this method is called ContainsKey. Here we are going to say that a user is looking for something we are going to create a variable right here of type string that we are going to call search and we are going to assign it the Console.ReadLine function to interact with it, I'm just going to add the ToLower method just in case he types a capital letter at the beginning. And here we are going to do what, we are going to say if in the dictionary there is the key that the user has entered then we display the value corresponding to the key otherwise we display The key that you have entered is not in the dictionary So here what do we do ? we do an if parentheses we put dic.ContainsKey the method that allows to see if a key is present in the dictionary and in the parentheses we put what the user is looking for or the variable that contains what the user is looking for to type. And to display we make a Console.WriteLine of the dictionary with the key that the user has entered And to finish, the else will process if the key is not present so we do an else braces inside Console.WriteLine and we display The key you typed is not present. I'm just going to do a little Console.WriteLine to tell the user to enter a key: hop and If we run the program, Enter a key: I type one that isn't in the dictionary I'm going to type clothing and the else g? Are now the exception if now I restart the program I type one that is present either vegetable, I enter and here we have the value that is affiliated with the key. Now let's delete all that and move on. We will now compare the power of dictionaries with lists. I'm going to quickly create a dictionary with as keys the first name of people and the value of their age. Either I will do var people = new dictionary tag the key will be of type string since it is names and this time for the value I can put the type int since it will be ages. But I will leave in type string for the continuation. Then I'm going to do several Add() people. Add parentheses the name I put Jason l age for example 26 I'm going to copy and paste as it is faster I just change the names here I put Thibaut 23 and the last Jibril and 18 Voila. Now if I want to display the value or the age of a person I just have to do a Console. WriteLine from the dictionary with a key like this right? Now, let's see what the list gives. Here we have two things to have the name and his age so here since we have two information per person we will have to keep them in a table or we will make a list which contains tables of 2 string type elements here is why just before I left the age as a string type because an array is strongly typed. So to do this, we're going to do a var list = new List tag and this is going to be a list of what? From string array so I do string brackets and parentheses; Then to add I make a list. add the first element we said it was here Jason so what do I add? I add an array of string brackets and braces to put Jason and his age is 26; You see? That is a syntax that we saw when we approached the notion of array here it is indeed an array of strings and here I am not obliged to put 2 because I directly initialize the values ​​in the braces. So here I have my first entry and here I am going to do a quick copy-paste to do the same thing with the others. Hop here I am finished we can continue Now how I could do to find the person that the user will enter. Well, we'll have to loop through the list, and once I find the person. Well, then I could get his age. To loop I will do a for int i = 0; i less than list.Count, Count for the number of elements in my list. And i++; Ok now, in the forum we will check if the name of the person is the name that the user has entered. I'm just going to do a Console.WriteLine at the top to tell it to enter a name and recreate a search variable which contains the Console.ReadLine() function. I want to compare the name to be the 1st element of the array so the index 0 here I will add brackets to recover it and I put 0 after it is == the name that the user has entered is search. Then I display his age, the age is the second element so I put this time 1 between the brackets. And we break to get out of the loop if he finds it. I launch the program I type Jibril and it shows me his age. But you are going to tell me why do all this? it comes down to the same thing as above, doesn?t it? Yes it is true however there is a fundamental difference between the dictionary and the list. Jason, Thibaut Jibril, and that's good it's Jibril so you go out. While for the dictionary, it is different. Here we didn't need to loop, we just gave the key in the Console.WriteLine() and we had direct access to the age of the person without having to loop. Here we only have 3 people but imagine we are on a database with millions of people, that means in this case if we implement our search algorithm with a list, it would be really very slow , because with each search we would be obliged to make a loop, to sweep almost all the data of the list until we find the person and that, it is not very optimized and effective. On the other hand, if we implemented our search algorithm with a dictionary, no matter how many people there are in the database, we will always have direct access which means that our program will be much faster and that is the power of the dictionary. This is very important, remember that when you are going to manipulate a large amount of data and you need to do searches on this data then always think of dictionaries, it will completely change the performance of your program, whether you are on a web or mobile application, it doesn't matter. As for tables and lists, we will see in the rest of this course projects for which we will have to use dictionaries and make much more advanced programs. In any case, I hope you liked this video and that everything was clear to you. See you next time in a new video.