So in this video, I have another legacy collection for you that you can watch, but you don't have
to.
And it's only relevant if you ever use legacy code or if you need to maintain legacy code, or you're
just looking up old projects where some code was written using hash tables.
They're just a collection type which has a key value pair similar to what a dictionary has, with the
difference that dictionaries are just a lot better in the sense that they have type safety, they have
null keys as well as concurrency, and that just makes dictionaries a better choice in any case.
Okay, so we're going to look at dictionaries in depth in the next videos.
But for now just watch the hash tables, just chill, speed it up if you're feeling bored, if it's
too slow for you, whatever.
You don't have to code along or whatever.
Just enjoy the ride.
And if you don't feel like watching hash tables at all, just jump to the next video.
So hash tables and dictionaries are some of the most useful collections in C sharp.
So we will see how to define them and how to use them.
And in which case scenario we should take advantage of them.
To understand the concept of hashing and tables.
First think about a real dictionary.
So assume we have a German and English dictionary, where every word in German corresponds to only one
word in English, because that would be limited the limitation here.
So when we try and translate the word auto, which in German basically translates into car, in English
we use one word as a key word that we can search for, for example.
And we got one translation as a result.
So there's always this 1 to 1 relationship.
So we have auto and that translates into car.
And we can achieve exactly that also using hash tables.
And we're going to use this in a very specific example here.
So let's say we have a student registration system where every student has an ID and many other data
such as name, age, grades and picture and so forth.
For such a system to work, we will need a special ID for every single student.
So it has to be unique.
If we were to store this data using a hash table or a dictionary, the student ID would be the key and
the student data would be an object that contains all of the other student info, and that would be
the value.
So we always have this key value pair okay.
So this is how you can find the actual data so to speak.
And this is the value that is behind it.
So you can see it like a locker.
So this is the ID of the locker or the location of the locker.
It could be different kinds of types of data.
It doesn't have to be an ID and this will be the value.
So in the dictionary they have to be of the same type.
In a hash table.
They can be of different types.
So this could be an integer.
And this could be an object for example.
So we're going to look at hash tables first.
Therefore we're going to create a new class.
Or actually I have prepared a new class for this.
Let's put it down here.
And it has a couple of properties.
It has an ID property a name property and the GPA.
And then we have a very simple constructor simply assigning the value.
So whatever ID we pass to the student object when we created will be the ID of the student object and
so forth.
Okay, so nothing too fancy here.
We're just using a normal class as we have covered in the class chapter.
Now we could of course go ahead and create this class in a separate file so we could go ahead and open
up our solution explorer.
Go ahead and create a new file here.
So add new item or a new class like so and call it student.
But for this example I'm going to keep it simple and put it into one file.
As you know, it's best practice to put it in a separate file so that you're not mixing things.
And you can easier find files and classes and all of that.
Okay, so now that we have our student class we can go ahead and create a hash table okay.
So inside of our program in here in the main method that's where I'm going to define a new hash table.
And it will be like so hash table.
And I'm going to call this one table which will be new hash table.
The hash table class is inside of System.collections.
So we need to add this namespace up here using System.collections.
So hash table and it's different to the hash tables namespace because that's how I called my which might
confuse our IDE here.
If we want to enter hash table without having this namespace added to our project.
But now that we have it inside of our programs class, we can use it.
So we have this new hash table, which will now be a table of data.
And we can put the data that we want in there.
So this is how you can create such a hash table.
How you can initialize it with an empty hash table object like we did here.
So now what we can do is we can assign values to this hash table.
And we could even call it students table to make our code a little more readable.
So now we just need to create a bunch of students that we want to have in our table.
So let me add those in here as well I'm going to call them step one, step two, step three, step four.
And they will all be new student objects.
So here this will be student with the ID one will be Maria.
She has an GPA of 98.
Then we have Jason which will have the ID of one.
This is this ID here, the unique identifier so to speak.
And then the GPA that the students have okay.
So now we have all of those students and we can assign them to our students table using the students
ID.
And a good practice would be to go ahead and say students table dot add.
And here we use the student one ID.
So this one here the property as the key.
So you can see here a hash table allows us to have a key of type object.
So it can be any type of object as the key.
And it can be an object optional or nullable as the value.
So we can assign a value or it can be null okay.
But we're going to assign a value and that will be the student one itself like so okay.
So now we added our student one to our students table.
And we can do the same thing with the other students.
And we're going to use their ID for each of them.
So let's use students table here for all of them okay.
So that's our hash table now.
But how can we fetch entries.
So how can we retrieve entries from our hash table.
Well let's look at different ways for it.
So the first thing that we can do is we can just create a student object here.
So student and this object will be called stored student okay.
So I'm going to get the student value from my table.
So in this case it's the student table.
Students table like so.
And here similar to how I access it in an array.
How I access a value in an array I can access it by an ID.
So here, for example, let's say I want to get the one at id one like so.
So here the problem is that this hash table can have values that are of type object right as we saw
earlier.
So here if we go ahead and call the add method you can see we can assign a key.
But then the value will be of type object.
And that means that this here will be of type object.
But we cannot assign just an object type to the student.
It has to be of type student because this stored student one is in fact of type student okay.
So we need to make sure that we cast it into a student here.
And then this will work.
Okay.
So we're casting this object into a student because we know that inside of the students table at position
one.
So at the ID one the value will be in fact of type student okay.
So here you can get the student at ID one.
You could get the student at ID two and so forth.
But that only works because we know that we are using in fact these numbers.
So if you want to get it otherwise you could use third one by ID.
So this will now give you the value behind the student one ID.
So this will basically give you this value here or this student here.
And it's this one to be precise.
So this one here because that's what we are storing at this ID one.
So now we can go ahead and do something like this where we now instead of write hello World, we are
going to actually write something useful where we write the student ID, the name of the student, as
well as his GPA.
So we're going to use stored student 1.id.
So we get his ID, we get his name and we get his GPA.
And in this case it's her GPA and so forth because it's Maria at position one at ID one okay.
So let's run this and you can see student ID is one name is Maria and GPA is 98.
So here we saw how we can create a hash table.
This is how we can store data in the hash table.
Actually this is where we store the data in a hash table.
And then this is how we can retrieve data from a hash table.
So now let's assume we want to actually print all the data inside of our.
Hash table, and we don't have a particular key at our disposal.
We can simply do that by getting all the keys from our hash table.
To solve this, first we have to discuss a struct called dictionary entry.
Whenever we add a new entry to our hash table.
So our key value pair and new dictionary entry object will be created for us, and it will get inserted
into our hash table, which means that a hash table is basically a collection of dictionary entries.
So using a temporary object of dictionary entry, we can go through our hash table using a for each
loop.
So let's look at this in this particular example here where I'm going to go ahead and use a for each
loop here.
And this for each loop type will be the dictionary entry.
That's the struct that I was talking about.
You can see here struct dictionary entry.
So what is the item name that I'm going to use.
I'm going to use entry.
Of course you could call this student or whatever because you know that the entries will be of type
student.
But let's say you don't know it.
And you can also call it entry.
And then what is the collection that we want to run this through or that we want to get the values from.
And that will be our students table.
So this one retrieve individual item with known ID.
And here retrieve all values from a hash table okay.
So an entry inside of our hash table is of type dictionary entry as I just said.
So this allows us now to go through this table the students table and basically display the values.
So here because entry dot value will return an object.
You can see here it says object.
But what we want in order to get the student details is going to be a student object.
What we'll need to do is we'll need to do the following.
So we create a temporary value which will be our entry value.
Now we have the same problem that we had up here okay.
Where we need to cast this into a student because it's just an object.
Right.
But we want it to be of type student so that we can access the student properties.
So because otherwise entry value dot it doesn't have ID here, I can't access its ID, I can't access
its name.
All of that stuff even though it exists in there.
But our IDE just doesn't allow that because we first need to make sure that we convert or cast the value
into the right type, which is going to be our type student.
So now we're casting it.
We're storing it temporarily, and now we can go ahead and write the individual values to our console,
for example, or using our program however we want.
So here we can use the student ID by using temp dot ID because temp now is of type student and student
has ID name GPA, and we can access all of those to.
Now iterate through the entire list of all of our students.
So let's do that real quick.
Let's run our code and see what this for each loop does for us.
So you can see it goes through it.
It says student for Steve.
Student three is Clara, student two is Jason, and one is Maria.
So you see, it goes from back to well, it starts at the very last entry and goes up to the first entry.
Now that is if at this point we don't know if it's a student, even though here we are converting it
into a student already.
This is really just to show you that a hash table is really just a collection of dictionary entries.
So let's look at how we can simplify this for each loop.
We can simplify it by actually saying for each where the variable type is going to be a student, the
item name, well you could call it student, I'm just going to call it value like so that's fine.
And the collection that I want to go through is going to be table well students tables dot values.
So there is this I collection of hash table values.
So it gets an I collection containing the values in the hash table.
So this allows us to go through the values directly without having to basically do what we did here,
where we converted it, or where we cast it into a student object.
So now we can just go ahead and do the same thing as we did here.
But now of course, it's going to be value.id and so forth, because it's not entry and it's not temp,
but it's going to be in fact value okay.
Because that's what this for each loop is giving us for each iteration of it, where it goes through
all of the values that we have and value is going to be the individual values.
That we're currently looking at for that particular iteration.
So in the first iteration it will be Steve, then it will be Clara, then Jason, then Maria.
Okay, so if we get rid of this line here or this for each loop and we run it again, we will see that
we get the same results.
So Steve, Clara, Jason and Maria okay.
So that was a quick introduction into hash tables.
In the next video I'll have a little challenge for you where you will basically extend your code a little
bit to understand hash tables a little better.
And then we're going to look at dictionaries.