‫And the last videos we talked about hash tables.
‫So in this lesson, we discuss how the generic version of hash tables looks like and the generic version
‫of hash table is dictionary.
‫So we're going to see how to use dictionaries in this video.
‫So similar to a hash table, a dictionary will store its value and a key value pair like we had with
‫the dictionary, right where we said the key value pair for auto would be car.
‫So the key would be auto, which is the German word for car.
‫And then car is going to be the value that is inside of this dictionary.
‫So that's a real world dictionary in a German English dictionary, for example.
‫But now we're going to use programming dictionaries.
‫So C-sharp dictionaries, which have a key value pair like we have here, where we store the value assigned
‫at a specific key.
‫So.
‫Let's go ahead and define a dictionary.
‫So from the Microsoft documentation, the dictionary is defined like so.
‫So it's going to look something like this dictionary rt value.
‫So what that basically means is that we need to specify the types of both our keys and values in the
‫declaration similar to how we did it, for example, for a list.
‫So a list also needed to have a specific type.
‫A list could be of type integer, it could be of type string, it could be of type object and so forth.
‫But we needed to define of what type this list has to be.
‫The same goes for dictionary, but there we have to define it for the key as well as for the value.
‫So that's basically what this key and RT value means.
‫So that means that we have a lot of flexibility, but not the flexibility that we had in a hash table.
‫So we basically can go ahead and create a dictionary like this one here where we say that the key will
‫be of type integer and the value will be of type string.
‫And of course, in order to use dictionaries, we need to make sure that we have using system that collections
‫are generic.
‫So you see a dictionary is a generic collection.
‫We saw in another video that there is a difference between generic and non generic collections.
‫So hash tables or non generic collections and dictionaries are generic collections, so they have all
‫of the advantages and disadvantages.
‫So now this dictionary will have an integer, a number, so to speak, and a value that will be a string.
‫So the number will be the key and the string will be the value.
‫Now here we're creating this dictionary called my dictionary, and we're initializing it with an empty
‫dictionary straight away.
‫At this point, we can then go ahead and use this dictionary so we can add values to that dictionary.
‫We can, for example, also already define the values of that dictionary straight away.
‫So in the same line, basically right here.
‫So we could, for example, just say, okay, this is going to be how the dictionary is going to look
‫like.
‫Where we have an integer and a string.
‫So we have the one being this integer here, the key and the one being the text for it would be the
‫value.
‫So now we have the integer type and then the spoken English word for whatever this integer is.
‫So one would be one, two would be two, three would be three and so forth.
‫Now, of course, dictionaries are not limited to integers and strings.
‫We can use much more complex values that we are storing.
‫So for example, let's consider this class here.
‫So I'm creating a new class called employee.
‫I'm going to put it in the same file, but of course you could create a separate file.
‫So we have a few properties like row, name, age and rate for every single employee.
‫And then we have the salary, which is a float, and it returns the rate of eight times, five times
‫four times 12.
‫So whatever the rate is, times, this value is going to be what we're going to store or get as the
‫salary.
‫So if we were to retrieve the salary, it will directly calculate the salary for us.
‫So that's 8 hours times five days times four weeks times 12 months.
‫So that's how it's going to calculate basically the yearly salary, so to speak.
‫Of course, times the rate.
‫So now let's say we have a database of employees.
‫Let's.
‫I assume that this is going to be our database of employees.
‫And by the way, I just realized that I missed the rate here when creating the constructor.
‫So this constructor is just going to also use the rate now and assign the rate here and now we can use
‫our data from the database.
‫So we have the database where we get the title, we get the name, we get the what is it, the age super
‫old lady, 95 years old.
‫And then we have her rate.
‫So she has a rate of 200.
‫Then we have the manager, Joe, who is 35, has a rate of 25 and so forth.
‫So now what I'm going to do is I'm not going to use this dictionary here.
‫I'm going to create a separate dictionary that I'm going to call employees directory.
‫And that employee directory will basically have a key value pair of string employee.
‫So it will store for a specific string the entire employee object.
‫And I'm going to call this employees directory and the string that we're going to use is going to be
‫the employees role.
‫Okay.
‫So here I'm going to use this for each loop that will now go through every single employee in my employees
‫database.
‫So this is my employees database.
‫I'm faking it, so to speak.
‫Right?
‫It's going to go through every single item of that employee state database and it's going to add to
‫my employees directory at the position or at the key of the role, the employee itself.
‫So here we're assuming that for every single role we only have one employee, otherwise this would screw
‫the whole system up.
‫So this is an example of how we can create a dictionary with data from a fake database, which is basically
‫just going to be an array of employees that we have defined here.
‫So now what if we want to get the data from a dictionary?
‫So we want to fetch data from a dictionary and let's say we want to fetch data of the employee that
‫is the CEO.
‫So that has the position of the CEO.
‫Then we can go employee I'm going to call this one Apple and we get it from the employees directory
‫and here we just say, okay, I want to have the CEO, so I want to know the person and maybe also the
‫entire object actually of the person that has CEO as their title.
‫Basically just saying the where the key is going to be CEO for my dictionary employee, stick to a directory
‫there.
‫I want to know who is the CEO and want to store the entire employee in an object.
‫So now I can go ahead and display the details of that employee using a console right line statement
‫like so.
‫So here I say the employee name will, which will be ample name, then the role as well as the salary.
‫So this will be displayed.
‫Okay.
‫So basically we'll get the salary based on the rate and this calculation here.
‫It will get the role directly and will get the name directly from the employee object itself.
‫So from this Apple object.
‫So now let's run this real quick and see what queen is going to earn, considering that she works full
‫time and we can see she has a salary of 384,000.
‫Her name is Gwen, her role as CEO.
‫So running this code works.
‫As you see, there's no problem here.
‫But the problem could be that we are entering a wrong value.
‫So let's say instead of CEO, I'm searching for the CTO, so for the technical officer, so the Chief
‫Technical Officer and if I run this, you see my application crashes.
‫So it says CTO was not present in the dictionary, so the key was never given.
‫So in order to fix that, we can basically check if the key exists.
‫So here what would be a very good way of checking.
‫This would be to create a key called SEO or string called key, which has the value of SEO.
‫And then we check the employees directory.
‫For that particular key.
‫So here you see contains key is one of the favorite or favorite options here.
‫So contains key.
‫And then here we can pass the string that we want to check as the key.
‫So does it contain the key called CEO?
‫If it does, then run this code here.
‫And if it doesn't, then don't run it.
‫Or you could, of course, also just say this key doesn't exist.
‫So now you could create a little application that is going to allow you to enter a key and then your
‫application will check.
‫Okay, does this exist or not?
‫And by the way, here this should be replaced with key.
‫It's not going to be hardcoded anymore.
‫Well, it's hardcoded in at another point, so to speak.
‫But this could be something that we get from user input or the user can select it from a UI or something
‫like that, and then the application would retrieve the data and display this instead of just in a console
‫application in an actual UI.
‫And you will later see how to use UI's or how to build your eyes using WPF.
‫So that is one way of getting around the problem.
‫Another way would be to use something called try get value.
‫So let me quickly add a little bit of code and go over it.
‫So let's say we have this empty employee, which I call result, and it's empty because I set it to
‫null and then I'm checking the employees directory.
‫If it has the value called intern and I'm using this try get value method.
‫So this try and get value method will return a boolean, which is great because that's what an IFF statement
‫likes to have in the condition and it will print out the result and it will put the value into result.
‫So this result will actually be an employee.
‫So try get value.
‫We'll try to get the value that we pass here.
‫And if it worked, the volume will return true.
‫So this statement will be executed and at the same time it will get what put the result into this variable
‫so the out will be put into this result variable.
‫So the value that we retrieve from our employees directory will be stored in result.
‫So that's what this statement here says.
‫And if it worked, in fact, and intern does exist, then this code will be executed and otherwise this
‫code will be executed.
‫So this key does not exist.
‫So that would be just another way of getting around the same issue.
‫So try get value also works well.
‫So value retrieved employee name Ernest Intern 22 and he gets a salary of €15,000 a year or whatever
‫type of money you want to talk about here.
‫Okay.
‫So now let's dig a little bit deeper because we can also get an element with the element at method,
‫so an element of our dictionary.
‫So as we learned or as you learned, a dictionary is going to use structs here.
‫So a dictionary is a collection of key value pairs, which is a struct that is defined like a key value
‫pair that is going to look something like this here.
‫So let's look at it this year.
‫So we have the key and the value.
‫So this is the key value pair.
‫So that's what a dictionary is containing.
‫So let's look at this for a loop here and let me I can actually put it right here.
‫So this for loop, what it will do is it will use this element add method.
‫However, in order to use it, we will need to implement the link.
‫So for now, just accept that link is a powerful tool that allows us to work with collections in a very
‫efficient way and we will have an extra chapter later on where we will look into link specifically,
‫but for now let's just accept it as that.
‫So once you get to the later chapters, you will learn more about it and then this will make more sense.
‫So in our case.
‫And this particular example where we have our key value pair with the key and value where key.
‫Is the type of our keys of our dictionary.
‫And the same goes for the RT value, which is the value of our dictionary.
‫Now in our case, it's string and employee if we look at it.
‫So for our dictionary that we created here, it's string and employee.
‫But internally behind this string key, there is also an integer value, so a number, so to speak.
‫So let's say at position zero will be the CEO at position one, the manager at position two, the air
‫person at position three, the secretary and so forth.
‫Even though they will all be at the position so at the key of their role, but internally they will
‫also be at a number.
‫So the key will also be a number internally.
‫So that's exactly what we can use for this element at.
‫So this element and if you hover over it returns the element at the specified index in a sequence.
‫So a dictionary is a sequence.
‫So it will return basically our employee position zero, our employee at position one, our amplitude
‫position two and so forth.
‫However, this will return a key value pair and the key value pair we are going to create here is going
‫to be of type, string and employee.
‫So now we can display the key and then we can get the value of our employee at our key value pair at
‫the value position.
‫Because we get it from our element at this is a little more complicated, but basically it is just another
‫way to access an item inside of your dictionary, not using the key as a string, but the key as an
‫integer.
‫And this goes for all different kinds of keys.
‫So no matter what type you used for the key, there is always a number assigned to it as well.
‫So even if this was off type object or if this was of type double or whatever, there is always an integer
‫assigned to it which is allowing us to use this element at at I.
‫So let's go through this and you will see that this will basically allow us to display all employees.
‫So it goes through all employees in our list.
‫So I believe it goes all the way up to here.
‫So it shows us all of the employees that we have in there.
‫And it even starts a little further up.
‫So this is basically the list that we are getting from this for loop.
‫So you can use a for loop to get through an entire list of dictionaries.
‫You can, of course, use A for each loop as well, but you can also access items using element eight
‫AI and then you can store it and an MP object because the value is going to be in fact off type employee
‫because that's what we defined here.
‫Now, if you look at the key, let's run it again.
‫You can see here, ki seo ki manager.
‫Ki h.r.
‫Ki secretary.
‫Now, we could also print the eye if we wanted to.
‫So here.
‫Like so, Trent.
‫Well, actually, let's just print it here.
‫I is and then I will be the second entry like so.
‫And then we print I.
‫Okay.
‫So then you can directly see which integer value is assigned to a specific position.
‫So the CEO is a position zero.
‫The manager is at position one, the HR person is a position two and so forth.
‫Okay.
‫All right.
‫So that's it for this video.
‫I think it's a long enough video to go over dictionaries, at least at this point.
‫So in the next video, we're going to look at how we can update an existing dictionary, so how we can
‫update the value of a dictionary and then also see how we can remove data from a dictionary.
‫So see you in the next video.