‫Welcome back.
‫And the last video we looked at dictionaries and we learn how to create dictionaries or assign values
‫to dictionaries, how to iterate through dictionaries.
‫We saw how to use this element AI in order to get the ID, so to speak, of a dictionary key item or
‫of each item.
‫And now we're going to look at how we can update and remove data from a dictionary.
‫So it's actually quite trivial.
‫So let's say we have the following situation.
‫We want to update a specific key.
‫So we want to update.
‫A position at a specific key, and that will be a little further down here.
‫So here we have the H.R. key.
‫That is the one that we want to update so we can check if there is, in fact, an employee in the directory
‫that contains this key.
‫So we check if this key to update in this case HR exists and if it exists, then we want to execute
‫some code.
‫And what we want to do is we want to replace Laura with a different employee.
‫So now Laura will be replaced with a new employee called in order to update an array.
‫We can just say the dictionary name, then the key name where we want to update something and then assigning
‫the value that we want to put into its place.
‫So at this position that is going to be the key.
‫h.R.
‫We are going to put this new employee object.
‫So we're going to say now instead of having the old person in hr, which was laura, we're going to
‫replace her with the younger Erika.
‫So that's what this code will do for us.
‫So that's how you can update an entry, basically dictionary name, key name in square brackets, and
‫then whatever you want to assign to it.
‫Now, the value that you're assigning to it has to, of course, follow the same type that the dictionary
‫was defined with.
‫So in our case, it has to be of type employee and this is in fact of type employee.
‫So now we can, of course also print that into our console like so employee withdrawal.
‫Key zero was updated at the position key to update.
‫So in this case ad hr was updated and then.
‫If that didn't work.
‫We can, of course, also print an error message here saying sorry the employee not found with this
‫key.
‫And like so.
‫And I have one too many, I think.
‫Like.
‫So this will.
‫Fix the error.
‫So let's run this real quick to see how we can update it.
‫And you see here at the very top, employee with role key HR was updated.
‫Now let's look at the HR person.
‫Where is she?
‫She's here.
‫It's like age 26 with a salary of 35,000 roughly.
‫Okay.
‫So the update worked and we can see in our list of all updates or all employees because that's where
‫we got all the employees from.
‫So now let's say we want to remove an item from our dictionary because here this is the for each group
‫that we use to add all of the items, all of the employees to our dictionary, then we edit it one.
‫So this is where we update and now let's look at how we can remove.
‫And in order to remove, we also need to have a string that will be the key that we want to replace
‫because well, we have a key value pair in our case that the key is a string and that's what we use
‫here.
‫And then we can also check if that actually works.
‫So the thing is the remove method, if you look at it, it actually has or it returns a boolean.
‫So hovering over remove, you will see returns a boolean, which means that it either removed it or
‫it didn't remove anything.
‫So if it removed something, what we're going to do is we're just going to say that at the key in this
‫case, in turn, the person was removed, maybe their internship was over, let's say after six months
‫or whatever.
‫Now we can delete that intern from our database.
‫So let's run this again.
‫In our case, not database, but dictionary.
‫Okay.
‫So here at the top, you see interne was removed and now let's see if we can find an intern.
‫And I believe we don't have an intern in our database here.
‫Okay, so we have the CEO, the HR person, the secretary, as well as the lead developer.
‫And now, of course, if it didn't work, we can have this statement.
‫So if no, then print an error message saying no employees file with this key.
‫And the key that we are looking at is the key to remove, which will be our internal key, which is
‫basically this one here.
‫So I hope you recall that we used the.
‫Role of the person as their key in the company.
‫So once again, this example is really just to demonstrate the features of C-sharp that you have here.
‫This is not how you would write this.
‫So I would not recommend to use the role as the key for your data structure, because the role is usually
‫something that many people can have.
‫So you can have multiple people having the role of intern.
‫You can have multiple lead developers in a company or multiple junior developers and so forth.
‫So that's something you have to be super wary about.
‫Now, I would always recommend to use an ID as the.
‫Key.
‫So something like an integer where you assign an ID to every single employee.
‫But this was just to show you that you have a lot of flexibility.
‫You can use strings as your keys, you can use integers as your keys and so forth.
‫All right.
‫So that's it for dictionaries for now.
‫See you in the next video.