‫In this lesson, we are going to cover hash tables and we're also going to look at dictionaries later
‫on.
‫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 limiting 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 is 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.
‫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 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 are 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 create it will be the ID of the student object
‫and so forth.
‫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.
‫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 a 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 ID 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 student's
‫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 stud one stat, 2 to 3, 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 a GPA of 98.
‫Then we have JSON 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 the good practice would be to go ahead and stay 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 knowable 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.
‫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 student's 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.
‫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 X is 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 off type student because this stored student one is in fact of type student.
‫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 student's table
‫at position one.
‫So at the ID one, the value will be in fact of type student.
‫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 fried 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 one dot 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, a 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.
‫An 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 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 will 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 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 ID 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 will want.
‫So here we can use the student ID by using template 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 four Steve.
‫STUDENT three Sclera.
‫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 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 while students tables dot values.
‫So there is this AI collection of hash table values.
‫So gets an AI 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 because that's what this for each group.
‫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 value 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.
‫So if we get rid of this line here or it is 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 and 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.