All right.
So let's keep going with our dictionaries here.
So in the last video we added items to our dictionary.
We saw how we can access items.
And obviously we also saw how to declare a dictionary.
Now let's see how we can update data inside of a dictionary.
Therefore I'm going to use employees and then the key inside of square brackets.
So for example at 102 I want to replace Bob Smith with Jane Smith okay.
So this is how you can update data in a dictionary.
Now what's important here is that this only works because we said that the key is of type int.
If the key were of a different type, we would have to enter whatever the type is for the key that we
want to change.
We're going to see that in a bit with strings.
Okay.
But for now I just want to go over it with very simple integers.
Now how can I remove an item from my dictionary.
Therefore I can use employees dot and there are a bunch of methods as you can see.
So you can add something, you can remove something.
And there are a bunch of other options that we have available.
So let's just use the remove method here.
And now here in brackets we need to indicate at which key we want to remove something.
Now we cannot use one here because at the key one there is nothing.
There is only something at 101 or 102 because that's where we added our employees.
So at these keys we added our employees.
So I can only delete them at the keys that we have available.
So this will remove 101.
Now let's just add a few more employees because otherwise we are ending up with just one employee there.
So I'm going to use a few more keys here.
So it will be Bob Smith, Rob Smith, Flop Smith and Bob Smith.
Okay, a bunch of Smith people all ending on Bob even though those names might not exist.
But then we have Jane Smith that we updated for our friend Bob Smith.
But now we have a few items at least.
And after removing one.
So this is how to remove an item from a dictionary.
How can we now go through all items of that dictionary?
How can we iterate through all of them?
Therefore, we can use a for each loop.
So for each loop is a little more complicated when it comes to using dictionaries.
So we need to use key value pair in employees.
So what is this key value pair.
Well it's also inside of System.collections.generic.
It is a struct.
We're going to see what structs are later on.
But it defines a key value pair that can be set or retrieved.
Now this is just a very simple dictionary.
You could say like a simplified dictionary.
And we're just saying int string as the two data types.
So int as the key and string as the value.
So that's what this key value pair comes from.
And it just calls it kvp in our dictionary.
So that's how we can now go through every single item.
So every single item inside of employees will be a key value pair.
So it will be a KVP.
Now we can go ahead and use the Console.WriteLine to look at each individual KVP.
However KVP is not a great name right.
So let's just call it employee.
So it's a little more readable.
So every single employee I want to have the ID of the employee which is our int.
So I'm going to use employee dot.
And you see we have value and we have key.
So we have these two properties that every single key value pair has available to it.
So I'm going to say the ID should be the key and the name should be the employee dot value okay.
Because that's how we are storing it right now.
We are storing the employee at the key with the value.
So with the name inside of our dictionary.
So this for each loop will go through all of those items.
So this John Doe is from earlier.
So we can get rid of this line here.
Now let's run this again and we will see.
We have Jane Smith, Rob Smith, Lob Smith and Bob Smith all at individual IDs.
So at 1 or 2 we have Jane at 1 or 3 we have Rob at 104 we have flop and so forth.
Now you see we don't have John Doe anymore because we removed him at this line here at line 20.
Six.
That's where we removed an item from the dictionary.
And with this for each loop, that's how we iterated through or over the dictionary.
So iterating iterating over a dictionary okay.
So that would be the starting point for our dictionaries.
Now there are a few things that we need to look at before we are going to look at another example of
dictionaries where we're going to use a string key with a value that will be of a different data type
as well.
So that's it for this video.
See you in the next one.