 Hey everybody, it's Brian. Welcome to the 11th C-Sharp tutorial. Today we're going to be discussing the hash table. First thing we need to do is actually add a namespace here, so let's go system.collections. The reason why you do that is because the hash table is not part of the generics namespace, we want the collections. So let's say hash table, we'll just call this mtable, and then of course we have to create an instance of it. And then we're just going to say mtable.add. Now, think of a hash table as a list, but each object has a key. Now, why would you want that? You have a bunch of objects, and let's just go out to Wikipedia here and look up hash table. And you see how we have a bunch of keys, and it has a hash function, and it puts it in these little buckets. What does all this mean? It means that each item you have has a unique key assigned to it. It does some sort of mathematical voodoo mumbo jumbo in here, and then it puts it in a nice orderly bucket so it can look it up very quickly. So if you're looking for items, say 14, it doesn't have to go through each and every single item, like a list would. Instead, it chops it where it thinks it's going to be, and then starts searching from there. So it's very quick to look things up, very handy if you have a huge list. So let's just add 100 and Brian. So what we're doing here is we are adding an item with a key of 100, and we'll say 101. So the basic syntax is here's our key, here's the object we're adding, and this could be anything. This could be a class, could be a string, could be an integer. Don't get caught up on the semantics just yet. You just need to know that you're adding a key and an item. Now that key is being hashed. And what do we mean by that? We mean we're doing that mathematical voodoo in the background, but it's all done in the background, so you don't have to worry about it. Then we're just going to do a 4-H loop. We're going to say int id in mtable. We want keys. So we want to just print out all the keys here. So we're going to say system, collections console, right line. And we're just going to print out the id. Let's go ahead and run this and see what happens. Oops, yes. We forgot our system console.read. Keep our window open here. Alright, now when we run this, you see how it says 101.100. Why did it do that? It put them kind of in the reverse. Well, it did that because it's creating a hash and it's showing you in the order the hashes are. See, like if we make another one here, we'll just call this 99 and we'll call this cat and run this. See, this says 101.100.99 because it's doing it from a high to a low order. Now you don't need to know any of that and it may jump things around based on how it's hashing it internally. And unfortunately, each language like Java, .NET, et cetera, et cetera, they do hash tables a slightly bit differently. But all you really need to know is you have a key and an object. So it does a very, very quick lookup. So what's the point of this key? I mean, what can you really do with it? Well, let's say you want to find item 101. You want to find Heather. How would you get that? Well, you could do like this. Let's just copy this. And we want to say values and then we want to do, actually it's a string. So we need to change this around. Say name and then print out the name here. And if it seems like it's going to be overly complex, it's because it is. And then you say if and then we'd have our brackets and then you get into this big long thing. Where you're basically saying if name equals Heather, then console, right line. You don't need any of this. That's my point. Just delete that. So if you just typed all that, I'm very sorry. But you don't need any of that. What you do is you'd say system console, right line, and we're going to say mtable. And we want item, what was it, 101? So we're just going to simply say we want item with the key of 101. And sure enough, it prints out Heather. So as you can see, you can do a very quick lookup based on that key. Some practical uses would be like, let's say you have a database of employees and you want to grab it based on the employee ID. You can do that. You should also note that you should do a little bit of error checking here and say, you notice how there's contains key and contains value and contains. Use contains key to see if it has that actual key. So we're just going to turn this into an if then statement. We're going to say if mtable contains key, 101, then print out the value of key 101. So we're saying does it contain the key yes or no? If it does, then do something. So there we go. And let's just give it an arbitrary key of like something like that, something we know doesn't exist. When we run this, sure enough, it does not print it out because it doesn't contain that key. So as you see, hash tables are very, very simple, very easy to work with. And almost every function with the hash table relates to the key. For example, let's say we want to remove something here. Let's say we want to remove Heather from the list. She's been bad. So we'll mtable, remove, and notice it says object key. So it's asking for that key again. So you give it the key and then run this. And notice how it's not printing out 101 anymore. And it's not printing out Heather. It's because we've removed her. So hash tables are very quick to look things up, very simple to work with. And almost everything works off this key. I'm not going to get too deep into this. I just want you to know what a hash table is and why you would use it. So that's all for this tutorial. I hope you found this educational and entertaining. And thank you for watching.