1 00:00:00,990 --> 00:00:03,600 ‫And the last videos we talked about hash tables. 2 00:00:03,600 --> 00:00:09,930 ‫So in this lesson, we discuss how the generic version of hash tables looks like and the generic version 3 00:00:09,930 --> 00:00:12,360 ‫of hash table is dictionary. 4 00:00:12,360 --> 00:00:15,120 ‫So we're going to see how to use dictionaries in this video. 5 00:00:15,210 --> 00:00:22,350 ‫So similar to a hash table, a dictionary will store its value and a key value pair like we had with 6 00:00:22,350 --> 00:00:30,210 ‫the dictionary, right where we said the key value pair for auto would be car. 7 00:00:30,540 --> 00:00:34,260 ‫So the key would be auto, which is the German word for car. 8 00:00:34,260 --> 00:00:38,580 ‫And then car is going to be the value that is inside of this dictionary. 9 00:00:39,240 --> 00:00:44,370 ‫So that's a real world dictionary in a German English dictionary, for example. 10 00:00:44,640 --> 00:00:48,180 ‫But now we're going to use programming dictionaries. 11 00:00:48,180 --> 00:00:57,360 ‫So C-sharp dictionaries, which have a key value pair like we have here, where we store the value assigned 12 00:00:57,360 --> 00:00:58,800 ‫at a specific key. 13 00:00:59,700 --> 00:01:00,240 ‫So. 14 00:01:01,040 --> 00:01:03,800 ‫Let's go ahead and define a dictionary. 15 00:01:03,950 --> 00:01:08,960 ‫So from the Microsoft documentation, the dictionary is defined like so. 16 00:01:09,500 --> 00:01:14,690 ‫So it's going to look something like this dictionary rt value. 17 00:01:14,870 --> 00:01:22,550 ‫So what that basically means is that we need to specify the types of both our keys and values in the 18 00:01:22,550 --> 00:01:27,560 ‫declaration similar to how we did it, for example, for a list. 19 00:01:27,860 --> 00:01:31,040 ‫So a list also needed to have a specific type. 20 00:01:31,040 --> 00:01:35,750 ‫A list could be of type integer, it could be of type string, it could be of type object and so forth. 21 00:01:35,780 --> 00:01:39,590 ‫But we needed to define of what type this list has to be. 22 00:01:39,620 --> 00:01:45,590 ‫The same goes for dictionary, but there we have to define it for the key as well as for the value. 23 00:01:45,920 --> 00:01:48,890 ‫So that's basically what this key and RT value means. 24 00:01:49,820 --> 00:01:55,880 ‫So that means that we have a lot of flexibility, but not the flexibility that we had in a hash table. 25 00:01:56,180 --> 00:02:04,610 ‫So we basically can go ahead and create a dictionary like this one here where we say that the key will 26 00:02:04,610 --> 00:02:09,170 ‫be of type integer and the value will be of type string. 27 00:02:10,300 --> 00:02:15,560 ‫And of course, in order to use dictionaries, we need to make sure that we have using system that collections 28 00:02:15,790 --> 00:02:16,600 ‫are generic. 29 00:02:16,720 --> 00:02:20,290 ‫So you see a dictionary is a generic collection. 30 00:02:20,830 --> 00:02:25,210 ‫We saw in another video that there is a difference between generic and non generic collections. 31 00:02:25,210 --> 00:02:31,450 ‫So hash tables or non generic collections and dictionaries are generic collections, so they have all 32 00:02:31,450 --> 00:02:33,640 ‫of the advantages and disadvantages. 33 00:02:35,500 --> 00:02:42,910 ‫So now this dictionary will have an integer, a number, so to speak, and a value that will be a string. 34 00:02:42,910 --> 00:02:46,990 ‫So the number will be the key and the string will be the value. 35 00:02:48,290 --> 00:02:53,630 ‫Now here we're creating this dictionary called my dictionary, and we're initializing it with an empty 36 00:02:53,630 --> 00:02:55,130 ‫dictionary straight away. 37 00:02:55,730 --> 00:03:03,260 ‫At this point, we can then go ahead and use this dictionary so we can add values to that dictionary. 38 00:03:04,170 --> 00:03:10,360 ‫We can, for example, also already define the values of that dictionary straight away. 39 00:03:10,380 --> 00:03:13,080 ‫So in the same line, basically right here. 40 00:03:13,080 --> 00:03:19,890 ‫So we could, for example, just say, okay, this is going to be how the dictionary is going to look 41 00:03:19,890 --> 00:03:20,430 ‫like. 42 00:03:23,490 --> 00:03:27,170 ‫Where we have an integer and a string. 43 00:03:27,180 --> 00:03:35,700 ‫So we have the one being this integer here, the key and the one being the text for it would be the 44 00:03:35,700 --> 00:03:36,420 ‫value. 45 00:03:36,600 --> 00:03:44,220 ‫So now we have the integer type and then the spoken English word for whatever this integer is. 46 00:03:44,640 --> 00:03:49,230 ‫So one would be one, two would be two, three would be three and so forth. 47 00:03:50,090 --> 00:03:53,180 ‫Now, of course, dictionaries are not limited to integers and strings. 48 00:03:53,180 --> 00:03:57,470 ‫We can use much more complex values that we are storing. 49 00:03:57,830 --> 00:04:00,620 ‫So for example, let's consider this class here. 50 00:04:00,620 --> 00:04:03,070 ‫So I'm creating a new class called employee. 51 00:04:03,080 --> 00:04:07,190 ‫I'm going to put it in the same file, but of course you could create a separate file. 52 00:04:07,250 --> 00:04:13,310 ‫So we have a few properties like row, name, age and rate for every single employee. 53 00:04:14,150 --> 00:04:21,560 ‫And then we have the salary, which is a float, and it returns the rate of eight times, five times 54 00:04:21,560 --> 00:04:22,820 ‫four times 12. 55 00:04:23,270 --> 00:04:30,830 ‫So whatever the rate is, times, this value is going to be what we're going to store or get as the 56 00:04:30,830 --> 00:04:31,580 ‫salary. 57 00:04:31,580 --> 00:04:37,520 ‫So if we were to retrieve the salary, it will directly calculate the salary for us. 58 00:04:38,350 --> 00:04:43,570 ‫So that's 8 hours times five days times four weeks times 12 months. 59 00:04:43,570 --> 00:04:47,980 ‫So that's how it's going to calculate basically the yearly salary, so to speak. 60 00:04:51,190 --> 00:04:52,570 ‫Of course, times the rate. 61 00:04:54,780 --> 00:04:58,050 ‫So now let's say we have a database of employees. 62 00:04:58,380 --> 00:04:58,890 ‫Let's. 63 00:04:59,770 --> 00:05:03,220 ‫I assume that this is going to be our database of employees. 64 00:05:03,670 --> 00:05:09,160 ‫And by the way, I just realized that I missed the rate here when creating the constructor. 65 00:05:09,160 --> 00:05:14,950 ‫So this constructor is just going to also use the rate now and assign the rate here and now we can use 66 00:05:14,980 --> 00:05:16,570 ‫our data from the database. 67 00:05:16,840 --> 00:05:24,280 ‫So we have the database where we get the title, we get the name, we get the what is it, the age super 68 00:05:24,280 --> 00:05:25,960 ‫old lady, 95 years old. 69 00:05:26,290 --> 00:05:29,110 ‫And then we have her rate. 70 00:05:29,110 --> 00:05:30,670 ‫So she has a rate of 200. 71 00:05:30,670 --> 00:05:34,720 ‫Then we have the manager, Joe, who is 35, has a rate of 25 and so forth. 72 00:05:37,270 --> 00:05:41,710 ‫So now what I'm going to do is I'm not going to use this dictionary here. 73 00:05:41,980 --> 00:05:47,440 ‫I'm going to create a separate dictionary that I'm going to call employees directory. 74 00:05:47,440 --> 00:05:53,590 ‫And that employee directory will basically have a key value pair of string employee. 75 00:05:53,590 --> 00:05:58,840 ‫So it will store for a specific string the entire employee object. 76 00:05:58,840 --> 00:06:05,770 ‫And I'm going to call this employees directory and the string that we're going to use is going to be 77 00:06:05,770 --> 00:06:08,350 ‫the employees role. 78 00:06:08,770 --> 00:06:09,200 ‫Okay. 79 00:06:09,400 --> 00:06:15,040 ‫So here I'm going to use this for each loop that will now go through every single employee in my employees 80 00:06:15,040 --> 00:06:15,700 ‫database. 81 00:06:15,700 --> 00:06:17,350 ‫So this is my employees database. 82 00:06:17,350 --> 00:06:18,700 ‫I'm faking it, so to speak. 83 00:06:18,700 --> 00:06:19,240 ‫Right? 84 00:06:19,240 --> 00:06:25,270 ‫It's going to go through every single item of that employee state database and it's going to add to 85 00:06:25,270 --> 00:06:34,900 ‫my employees directory at the position or at the key of the role, the employee itself. 86 00:06:35,290 --> 00:06:41,710 ‫So here we're assuming that for every single role we only have one employee, otherwise this would screw 87 00:06:41,710 --> 00:06:42,910 ‫the whole system up. 88 00:06:43,120 --> 00:06:50,020 ‫So this is an example of how we can create a dictionary with data from a fake database, which is basically 89 00:06:50,020 --> 00:06:54,670 ‫just going to be an array of employees that we have defined here. 90 00:06:55,660 --> 00:06:58,690 ‫So now what if we want to get the data from a dictionary? 91 00:06:58,690 --> 00:07:06,130 ‫So we want to fetch data from a dictionary and let's say we want to fetch data of the employee that 92 00:07:06,130 --> 00:07:07,480 ‫is the CEO. 93 00:07:07,480 --> 00:07:10,210 ‫So that has the position of the CEO. 94 00:07:10,540 --> 00:07:18,700 ‫Then we can go employee I'm going to call this one Apple and we get it from the employees directory 95 00:07:19,240 --> 00:07:27,430 ‫and here we just say, okay, I want to have the CEO, so I want to know the person and maybe also the 96 00:07:27,460 --> 00:07:33,310 ‫entire object actually of the person that has CEO as their title. 97 00:07:33,310 --> 00:07:42,580 ‫Basically just saying the where the key is going to be CEO for my dictionary employee, stick to a directory 98 00:07:42,670 --> 00:07:43,060 ‫there. 99 00:07:43,060 --> 00:07:49,390 ‫I want to know who is the CEO and want to store the entire employee in an object. 100 00:07:49,540 --> 00:07:56,950 ‫So now I can go ahead and display the details of that employee using a console right line statement 101 00:07:56,950 --> 00:07:57,730 ‫like so. 102 00:07:57,790 --> 00:08:05,230 ‫So here I say the employee name will, which will be ample name, then the role as well as the salary. 103 00:08:05,230 --> 00:08:07,000 ‫So this will be displayed. 104 00:08:07,600 --> 00:08:07,980 ‫Okay. 105 00:08:08,020 --> 00:08:13,390 ‫So basically we'll get the salary based on the rate and this calculation here. 106 00:08:13,540 --> 00:08:21,400 ‫It will get the role directly and will get the name directly from the employee object itself. 107 00:08:21,400 --> 00:08:23,410 ‫So from this Apple object. 108 00:08:24,430 --> 00:08:30,160 ‫So now let's run this real quick and see what queen is going to earn, considering that she works full 109 00:08:30,160 --> 00:08:34,720 ‫time and we can see she has a salary of 384,000. 110 00:08:34,840 --> 00:08:37,510 ‫Her name is Gwen, her role as CEO. 111 00:08:38,470 --> 00:08:41,020 ‫So running this code works. 112 00:08:41,020 --> 00:08:42,910 ‫As you see, there's no problem here. 113 00:08:42,910 --> 00:08:46,990 ‫But the problem could be that we are entering a wrong value. 114 00:08:46,990 --> 00:08:53,980 ‫So let's say instead of CEO, I'm searching for the CTO, so for the technical officer, so the Chief 115 00:08:53,980 --> 00:08:58,570 ‫Technical Officer and if I run this, you see my application crashes. 116 00:08:58,570 --> 00:09:03,490 ‫So it says CTO was not present in the dictionary, so the key was never given. 117 00:09:03,790 --> 00:09:09,850 ‫So in order to fix that, we can basically check if the key exists. 118 00:09:10,180 --> 00:09:13,570 ‫So here what would be a very good way of checking. 119 00:09:13,570 --> 00:09:21,880 ‫This would be to create a key called SEO or string called key, which has the value of SEO. 120 00:09:21,880 --> 00:09:25,540 ‫And then we check the employees directory. 121 00:09:26,220 --> 00:09:27,670 ‫For that particular key. 122 00:09:27,690 --> 00:09:34,110 ‫So here you see contains key is one of the favorite or favorite options here. 123 00:09:34,110 --> 00:09:35,750 ‫So contains key. 124 00:09:35,760 --> 00:09:41,040 ‫And then here we can pass the string that we want to check as the key. 125 00:09:41,040 --> 00:09:44,400 ‫So does it contain the key called CEO? 126 00:09:44,850 --> 00:09:48,510 ‫If it does, then run this code here. 127 00:09:49,050 --> 00:09:51,420 ‫And if it doesn't, then don't run it. 128 00:09:51,420 --> 00:09:56,670 ‫Or you could, of course, also just say this key doesn't exist. 129 00:09:56,670 --> 00:10:03,450 ‫So now you could create a little application that is going to allow you to enter a key and then your 130 00:10:03,450 --> 00:10:04,440 ‫application will check. 131 00:10:04,440 --> 00:10:06,240 ‫Okay, does this exist or not? 132 00:10:06,240 --> 00:10:10,680 ‫And by the way, here this should be replaced with key. 133 00:10:11,160 --> 00:10:13,080 ‫It's not going to be hardcoded anymore. 134 00:10:13,080 --> 00:10:15,720 ‫Well, it's hardcoded in at another point, so to speak. 135 00:10:15,720 --> 00:10:21,630 ‫But this could be something that we get from user input or the user can select it from a UI or something 136 00:10:21,630 --> 00:10:27,330 ‫like that, and then the application would retrieve the data and display this instead of just in a console 137 00:10:27,330 --> 00:10:29,190 ‫application in an actual UI. 138 00:10:29,280 --> 00:10:34,560 ‫And you will later see how to use UI's or how to build your eyes using WPF. 139 00:10:37,200 --> 00:10:40,810 ‫So that is one way of getting around the problem. 140 00:10:40,830 --> 00:10:46,290 ‫Another way would be to use something called try get value. 141 00:10:46,830 --> 00:10:50,610 ‫So let me quickly add a little bit of code and go over it. 142 00:10:50,730 --> 00:10:56,030 ‫So let's say we have this empty employee, which I call result, and it's empty because I set it to 143 00:10:56,040 --> 00:10:59,820 ‫null and then I'm checking the employees directory. 144 00:10:59,910 --> 00:11:05,270 ‫If it has the value called intern and I'm using this try get value method. 145 00:11:05,280 --> 00:11:09,720 ‫So this try and get value method will return a boolean, which is great because that's what an IFF statement 146 00:11:09,720 --> 00:11:20,330 ‫likes to have in the condition and it will print out the result and it will put the value into result. 147 00:11:20,340 --> 00:11:23,280 ‫So this result will actually be an employee. 148 00:11:23,430 --> 00:11:25,710 ‫So try get value. 149 00:11:25,740 --> 00:11:28,530 ‫We'll try to get the value that we pass here. 150 00:11:28,530 --> 00:11:31,530 ‫And if it worked, the volume will return true. 151 00:11:31,530 --> 00:11:40,560 ‫So this statement will be executed and at the same time it will get what put the result into this variable 152 00:11:40,560 --> 00:11:44,340 ‫so the out will be put into this result variable. 153 00:11:44,820 --> 00:11:50,490 ‫So the value that we retrieve from our employees directory will be stored in result. 154 00:11:50,880 --> 00:11:53,130 ‫So that's what this statement here says. 155 00:11:53,280 --> 00:12:00,420 ‫And if it worked, in fact, and intern does exist, then this code will be executed and otherwise this 156 00:12:00,420 --> 00:12:01,530 ‫code will be executed. 157 00:12:01,530 --> 00:12:03,290 ‫So this key does not exist. 158 00:12:03,300 --> 00:12:07,380 ‫So that would be just another way of getting around the same issue. 159 00:12:07,740 --> 00:12:11,640 ‫So try get value also works well. 160 00:12:11,640 --> 00:12:21,060 ‫So value retrieved employee name Ernest Intern 22 and he gets a salary of €15,000 a year or whatever 161 00:12:21,090 --> 00:12:23,760 ‫type of money you want to talk about here. 162 00:12:24,450 --> 00:12:24,680 ‫Okay. 163 00:12:24,780 --> 00:12:33,570 ‫So now let's dig a little bit deeper because we can also get an element with the element at method, 164 00:12:33,690 --> 00:12:36,960 ‫so an element of our dictionary. 165 00:12:37,290 --> 00:12:43,650 ‫So as we learned or as you learned, a dictionary is going to use structs here. 166 00:12:43,740 --> 00:12:50,280 ‫So a dictionary is a collection of key value pairs, which is a struct that is defined like a key value 167 00:12:50,280 --> 00:12:54,290 ‫pair that is going to look something like this here. 168 00:12:54,300 --> 00:12:56,490 ‫So let's look at it this year. 169 00:12:56,730 --> 00:12:59,430 ‫So we have the key and the value. 170 00:12:59,430 --> 00:13:01,740 ‫So this is the key value pair. 171 00:13:01,830 --> 00:13:04,710 ‫So that's what a dictionary is containing. 172 00:13:05,100 --> 00:13:12,030 ‫So let's look at this for a loop here and let me I can actually put it right here. 173 00:13:12,330 --> 00:13:18,450 ‫So this for loop, what it will do is it will use this element add method. 174 00:13:18,750 --> 00:13:22,530 ‫However, in order to use it, we will need to implement the link. 175 00:13:22,530 --> 00:13:30,600 ‫So for now, just accept that link is a powerful tool that allows us to work with collections in a very 176 00:13:30,600 --> 00:13:36,810 ‫efficient way and we will have an extra chapter later on where we will look into link specifically, 177 00:13:36,810 --> 00:13:39,840 ‫but for now let's just accept it as that. 178 00:13:40,080 --> 00:13:45,450 ‫So once you get to the later chapters, you will learn more about it and then this will make more sense. 179 00:13:46,050 --> 00:13:48,870 ‫So in our case. 180 00:13:49,590 --> 00:13:57,450 ‫And this particular example where we have our key value pair with the key and value where key. 181 00:13:58,700 --> 00:14:01,670 ‫Is the type of our keys of our dictionary. 182 00:14:01,940 --> 00:14:07,880 ‫And the same goes for the RT value, which is the value of our dictionary. 183 00:14:08,060 --> 00:14:13,520 ‫Now in our case, it's string and employee if we look at it. 184 00:14:13,850 --> 00:14:18,580 ‫So for our dictionary that we created here, it's string and employee. 185 00:14:18,590 --> 00:14:27,080 ‫But internally behind this string key, there is also an integer value, so a number, so to speak. 186 00:14:27,590 --> 00:14:34,910 ‫So let's say at position zero will be the CEO at position one, the manager at position two, the air 187 00:14:34,940 --> 00:14:38,360 ‫person at position three, the secretary and so forth. 188 00:14:39,380 --> 00:14:46,490 ‫Even though they will all be at the position so at the key of their role, but internally they will 189 00:14:46,490 --> 00:14:48,890 ‫also be at a number. 190 00:14:48,890 --> 00:14:51,800 ‫So the key will also be a number internally. 191 00:14:52,190 --> 00:14:56,450 ‫So that's exactly what we can use for this element at. 192 00:14:56,870 --> 00:15:04,400 ‫So this element and if you hover over it returns the element at the specified index in a sequence. 193 00:15:05,120 --> 00:15:07,610 ‫So a dictionary is a sequence. 194 00:15:07,610 --> 00:15:15,100 ‫So it will return basically our employee position zero, our employee at position one, our amplitude 195 00:15:15,200 --> 00:15:16,670 ‫position two and so forth. 196 00:15:17,330 --> 00:15:24,590 ‫However, this will return a key value pair and the key value pair we are going to create here is going 197 00:15:24,590 --> 00:15:26,150 ‫to be of type, string and employee. 198 00:15:26,900 --> 00:15:37,190 ‫So now we can display the key and then we can get the value of our employee at our key value pair at 199 00:15:37,190 --> 00:15:38,330 ‫the value position. 200 00:15:39,230 --> 00:15:47,660 ‫Because we get it from our element at this is a little more complicated, but basically it is just another 201 00:15:47,660 --> 00:15:57,770 ‫way to access an item inside of your dictionary, not using the key as a string, but the key as an 202 00:15:57,770 --> 00:15:58,160 ‫integer. 203 00:15:58,160 --> 00:16:00,680 ‫And this goes for all different kinds of keys. 204 00:16:00,680 --> 00:16:06,830 ‫So no matter what type you used for the key, there is always a number assigned to it as well. 205 00:16:07,100 --> 00:16:13,670 ‫So even if this was off type object or if this was of type double or whatever, there is always an integer 206 00:16:13,670 --> 00:16:18,950 ‫assigned to it which is allowing us to use this element at at I. 207 00:16:19,310 --> 00:16:26,210 ‫So let's go through this and you will see that this will basically allow us to display all employees. 208 00:16:26,420 --> 00:16:29,240 ‫So it goes through all employees in our list. 209 00:16:29,240 --> 00:16:31,550 ‫So I believe it goes all the way up to here. 210 00:16:31,880 --> 00:16:35,210 ‫So it shows us all of the employees that we have in there. 211 00:16:37,330 --> 00:16:39,660 ‫And it even starts a little further up. 212 00:16:39,670 --> 00:16:45,190 ‫So this is basically the list that we are getting from this for loop. 213 00:16:45,730 --> 00:16:50,230 ‫So you can use a for loop to get through an entire list of dictionaries. 214 00:16:50,230 --> 00:16:55,780 ‫You can, of course, use A for each loop as well, but you can also access items using element eight 215 00:16:55,860 --> 00:17:03,220 ‫AI and then you can store it and an MP object because the value is going to be in fact off type employee 216 00:17:03,250 --> 00:17:05,050 ‫because that's what we defined here. 217 00:17:05,760 --> 00:17:08,070 ‫Now, if you look at the key, let's run it again. 218 00:17:09,710 --> 00:17:14,060 ‫You can see here, ki seo ki manager. 219 00:17:14,090 --> 00:17:15,470 ‫Ki h.r. 220 00:17:15,740 --> 00:17:17,000 ‫Ki secretary. 221 00:17:17,270 --> 00:17:21,470 ‫Now, we could also print the eye if we wanted to. 222 00:17:21,500 --> 00:17:22,400 ‫So here. 223 00:17:24,370 --> 00:17:26,580 ‫Like so, Trent. 224 00:17:26,710 --> 00:17:29,080 ‫Well, actually, let's just print it here. 225 00:17:30,100 --> 00:17:35,320 ‫I is and then I will be the second entry like so. 226 00:17:35,560 --> 00:17:38,020 ‫And then we print I. 227 00:17:38,870 --> 00:17:39,190 ‫Okay. 228 00:17:39,200 --> 00:17:44,690 ‫So then you can directly see which integer value is assigned to a specific position. 229 00:17:44,690 --> 00:17:46,670 ‫So the CEO is a position zero. 230 00:17:46,700 --> 00:17:52,730 ‫The manager is at position one, the HR person is a position two and so forth. 231 00:17:52,820 --> 00:17:53,370 ‫Okay. 232 00:17:54,170 --> 00:17:54,590 ‫All right. 233 00:17:54,590 --> 00:17:55,730 ‫So that's it for this video. 234 00:17:55,730 --> 00:18:00,140 ‫I think it's a long enough video to go over dictionaries, at least at this point. 235 00:18:00,140 --> 00:18:06,050 ‫So in the next video, we're going to look at how we can update an existing dictionary, so how we can 236 00:18:06,050 --> 00:18:11,510 ‫update the value of a dictionary and then also see how we can remove data from a dictionary. 237 00:18:12,080 --> 00:18:13,250 ‫So see you in the next video.