Welcome back.
In this video we're going to look at dictionaries.
At least we're going to start looking at them because they are so powerful.
And there are so many facets that we need to look at, so many individual aspects that the dictionary
has to offer, that it's going to be multiple videos.
So let's build a dictionary and then understand what the dictionary is all about and why it matters.
Because the general idea of dictionaries is that they are allowing us to use collections with a key
value pair.
So let's look at what this means.
So I can use this keyword dictionary here.
And this dictionary looks like this.
So we need to define which key and value pair we want to have.
So the key value the idea is that for each key you have a value assigned to it.
You cannot have the same key multiple times.
The same key only exists once.
This allows us to always know that at a certain.
Let's say ID for example, in a database there is a certain object stored in there or a certain name.
So this would work very well for something like employees.
Right.
So key value this is what this t key and t value is.
Now you see here if you hover over the dictionary you see that it is inside of System.collections.generic.
And that's where the dictionary class is.
And then in pointy brackets it says t t value where t key is not null.
So this t means that it's generic which means that it can be of any type.
So whatever type you want to use, you can use that as the key as well as as the value.
However you must make sure that the key is not a nullable type.
It cannot be null.
All right.
So let's make it very simple by just using a int string key value pair okay.
So I'm going to call this one employee because it makes it a little simpler.
So employees is plural because it's a collection right.
So we will have multiple items of employees inside of this dictionary.
Now you see how you declare the dictionaries like this.
You use the dictionary keyword.
Then in pointy brackets you set the key type as well as the comma.
Then the value type.
You close the bracket, you give it a name, and then you initialize it in the next line where you use
the new keyword, the dictionary keyword, and then the same data types that you used earlier.
You add them in here as well.
Then you have the opening and closing rounded brackets and the semicolon.
So here we are declaring and initializing a dictionary.
Now in order to add an item we can just say employees dot add.
And then we need to indicate at which key we want to assign which value.
So here the key is of type int as we defined it this way.
So let's say add 101 I want to have John Doe.
So at this ID of 101 I'm going to have John Doe in my employees dictionary.
So in my collection of employees now let's add another employee in here.
So another add and this one will be 102.
And here we're going to have Bob Smith for example okay.
So now this is how we can very easily create more and more entries inside of our dictionary collection.
So it's like a list, but it is more complex in the sense that it is a key value pair and you have full
control over it.
So you have control over at which key you store which value.
So how can we now access the employee data?
We can access it similarly to what we did with arrays.
However, it just looks like this.
Because we are using an int as the key.
We're going to see what alternatives we have.
So I can go ahead and just say string name is going to be employee at the position of 101 or employees
to be more precise.
So I'm going to get the employee at the position 101.
So at this index or this int 101.
And then I can just type out the name okay.
So I'm just going to run this and you will see it will say John Doe.
Because at 101 that's where we have John Doe.
There we are.
So this here is how we access items in a dictionary okay.
And I'd say let's make a pause here.
And in the next video we're going to see how we can update and how we can check whether there is data,
as well as how we can remove data from our collection dictionary.