All right.
So in this video we're going to look at how we can store entire objects inside of a dictionary instead
of just strings okay.
So in our example with employees we just use strings here.
But what if we had another class.
So here I'm going to have another class called employee.
And now this class will contain certain information.
For example we could have the name.
So this would just be a property string and name.
Then we could have the age or date of birth or whatever.
So public string and well this would be an int let's say age.
Then we have the public int salary like so okay.
So we have all of this information about an employee.
And what we're going to say is that each time that we create a employee object, we need to pass all
of this information.
So we need to pass the name the age as well as the salary.
So salary like so.
And now let's just assign all of those values age, salary and so forth okay.
So now let's go ahead and use this employee.
So instead of just saying okay our employees are going to be a dictionary of in strings.
They're actually going to be a dictionary of employees.
So now obviously we need to change that here as well.
And let's get rid of all the other code that we created so far okay.
So you see the difference straight away.
So now we're saying at the key of an int we are going to store an entire employee object.
So now if we want to add an item to employees what we can do is we can say employees dot add.
And then we need to say at which index we want to add, as well as the employee that we want to add.
So here we could say new employee.
And then we need to pass the name the age as well as the salary.
So John Doe for example.
Then he is 35 and he has a salary of let's say 100 K a year.
Okay.
So this is how we can now add an item to our dictionary, which isn't simplified to just have an int
and a string associated to it, but it actually can hold entire objects.
So let's just go through this and add a few more items.
So at index two or at the int two it's not the index, it's the key right at key two I'm going to add
John does okay.
So John does.
And let's add John doesn't.
And John doesn't is uh 25.
And he earns quite a bit more.
So for some reason uh and then we have John wasn't at and he is 45.
He wasn't and he never will be.
And his salary will be let's say 80 K.
And then we have John won't and John or let's call him Will.
John Will and he's still 15, so he already gets 50 K quite a good guy.
Now I just need to make sure that obviously my keys are unique each time.
And now how could we iterate through each employee and give us all the information of each employee?
So let's just create a for each loop.
And here you can see it gives me item in employees.
So this is obviously easier than creating a key value pair.
Or just saying that each item is going to be a key value pair.
So this would have worked for the other one as well, where we're just using the var keyword and we're
saying an item of employees, whatever type that will be.
Let's just go through it individually.
So what this item will be will be every single entry inside of my dictionary.
So every single key value pair you can hover over this var and you will see of which type it is.
And it directly says here it's generic key value pair.
So that's what we saw last time.
So you can see the var really makes it easier sometimes at least coding wise.
And then what we want to do is we want to print out the infos about each employee.
So what we can say is ID and then we use item dot.
And here we need to use the curly brackets.
So item dot key.
And now how can we actually get the employee name or the employee salary or age.
How would you think to access that well named.
Let me show you how that works.
So we use the curly brackets once again.
And here we're using item dot value dot.
And then you see we have.
All the properties of our employee, because you can see the employee here, the employee is the data
type that we store as the value.
So whenever we're accessing the value we can store the values properties.
So here you see value dot gives us the different properties that we have available in our employee.
So we have the name.
Now how would you access the salary okay earns.
And maybe yeah we have an empty space there okay.
So we can just say earns and then curly brackets again we say item dot value dot salary and then end
is so and so many years old and is item dot value dot age years old.
Okay.
So this is how we iterate through our entire dictionary that now has a complex data type as the value.
Okay, we still kept the key simple just an ID or an int.
We can make it more complex however.
So let's still run this very quickly and see what's going to come from it.
And we see ID one named John does Ernst 100,000 and is 35 years old and so forth.
So it really goes through the entire list.
And I'd say this is good enough for this video.
This is just another level of what you can do with dictionaries.
So you could and that becomes a lot more complicated.
You could even use an employee object as the key instead of just an int as the key.
Obviously an int as the key.
You could say, well, why wouldn't I just use arrays then?
Because arrays can do the same thing.
I can have an employee inside of an array, or like I can have multiple employees of an array inside
of an array.
So that would be a collection that works as well.
Why not use that?
Well, the thing is, actually the answer to this question is a little more complex and longer than
I would like to just say in a few sentences.
So I'm going to add a text lecture where you can read through it in depth, and that will give you a
significantly better understanding of why it is still useful to do it this way and using dictionaries.
All right.
So I'd say let's look at how we can use dictionaries with a string as the key and a string as a value
as well.