📚What is a Dictionary in C#?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube in this video you are going to learn about man actually no you're going to learn about dictionaries and without dictionaries i wouldn't be able to read this manga because it would be too complicated to read it in japanese and i don't know all of the words yet so i need a dictionary and you will need dictionaries too if you want to become a good c-sharp developer so that's what this video will be about i hope you will enjoy it and if you do then please hit that like button that really helps us out and yeah it helps us to create more amazing content in the future so let's check out what dictionaries can do for you how you can use them and a lot more let's get started in this lesson we'll discuss how the generic version of hash tables looks like and the generic version of hash table is dictionary in case you're wondering what a hash table is check out our video on hash tables you'll find the link in the info card so we're going to see how to use dictionaries in this video so similar to a hash table a dictionary will store its value and a key value pair the key value pair for auto would be car okay so the key would be auto which is the german word for car and then car is going to be the value that is inside of this dictionary okay so that's a real world dictionary a german english dictionary for example but now we are going to use programming dictionaries so c-sharp dictionaries which have a key value pair like we have here where we store the value assigned at a specific key okay so let's go ahead and define a dictionary so from the microsoft documentation the dictionary is defined like so dictionary t key t value so what that basically means is that we need to specify the types of both our keys and values in the declaration similar to how we did it for example for a list okay so a list also needed to have a specific type a list could be of type integer it could be of type string it could be of type object and so forth but we needed to define of what type this list has to be the same goes for dictionary but there we have to define it for the key as well as for the value okay so that's basically what this t and t value means okay so that means that we have a lot of flexibility but not the flexibility that we had in a hash table so we basically can go ahead and create a dictionary like this one here where we say that the key will be of type integer and the value will be of type string and of course in order to use dictionaries we need to make sure that we have using system.collections.generic so you see a dictionary is a generic collection we saw in another video that there is a difference between generic and non-generic collections so hash tables are non-generic collections and dictionaries are generic collections so they have all of the advantages and disadvantages so now this dictionary will have an integer a number so to speak and a value that will be a string so the number will be the key and the string will be the value now here we're creating this dictionary called my dictionary and we're initializing it with an empty dictionary straight away at this point we can then go ahead and use this dictionary so we can add values to that dictionary we can for example also already define the values of that dictionary straight away so in the same line basically right here so we could for example just say okay this is going to be how the dictionary is going to look like where we have an integer and a string so we have the one being this integer here the key and the one being the text for it would be the value so now we have the integer type and then the spoken english word for whatever this integer is okay so one would be one two would be two three would be three and so forth now of course dictionaries are not limited to integers and strings we can use much more complex values that we are storing okay so for example let's consider this class here so i'm creating a new class called employee i'm going to put it in the same file but of course you could create a separate file so we have a few properties like row name age and rate for every single employee okay and then we have the salary which is a float and it returns the rate of 8 times 5 times 4 times 12. okay so whatever the rate is times this value is going to be what we're going to store or we get as the salary so if we were to retrieve the salary it will directly calculate the salary for us so that's eight hours times five days times four weeks times 12 months so that's how it's going to calculate basically the yearly salary so to speak of course times the rate so now let's say we have a database of employees okay let's assume that this is going to be our database of employees and by the way i just realized that i missed the rate here when creating the constructor so this constructor is just going to also use the rate now and assign the rate here and now we can use our data from the database okay so we have the database where we get the title we get the name we get the what is it the age super old lady 95 years old and then we have her rate so she's a rate of 200 then we have the manager joe he's 35 has a rate of 25 and so forth quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c master class in which you are going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below so now what i'm going to do is i'm not going to use this dictionary here i'm going to create a separate dictionary that i'm going to call employees directory and that employees directory will basically have a key value pair of string employee so it will store for a specific string the entire employee object and i'm going to call this employees directory okay and the string that we're going to use is going to be the employees role okay so here i'm going to use this for each loop that will now go through every single employee in my employees database so this is my employees database i'm faking it so to speak right it's going to go through every single item of that employee database and it's going to add to my employees directory at the position or at the key of the role the employee itself okay so for here we're assuming that for every single role we only have one employee otherwise this would screw the whole system up so this is an example of how we can create a dictionary with data from a fake database which is basically just going to be an array of employees that we have defined here so now what if we want to get the data from a dictionary so we want to fetch data from a dictionary and let's say we want to fetch data of the employee that is the ceo so that has the position of the ceo then we can go employee i'm going to call this one ample and we get it from the employees directory and here we just say okay i want to have the ceo so i want to know the person and maybe also the entire object actually of that person that has cuo as their title basically just saying that where the key is going to be ceo for my dictionary employee logistic directory there i want to know who is the ceo and want to store the entire employee in an object so now i can go ahead and display the details of that employee using a console.writeline statement like so so here i say the employee name will which will be ample name then the role as well as the salary so this will be displayed okay so basically it will get the salary based on the rate and this calculation here it will get the roll directly and we'll get the name directly from the employee object itself so from this ample object okay so now let's run this real quick and see what quinn is going to earn considering that she works full time and we can see she has a salary of 384 000 her name is gwyn her role as ceo so running this code works as you see there's no problem here but the problem could be that we are entering a wrong value so let's say instead of ceo i'm searching for the cto so for the technical officer so the chief technical officer and if i run this you see my application crashes so it says cto was not present in the dictionary so the key was never given so in order to fix that we can basically check if the key exists so here what would be a very good way of checking this would be to create a key called co or string called key which has the value of ceo and then we check the employees directory for that particular key so here you see contains key is one of the favorite or favorized options here so contains key and then here we can pass the string that we want to check as the key so does it contain the key called ceo if it does then run this code here and if it doesn't then don't run it or you could of course also just say this key doesn't exist so now you could create a little application that is going to allow you to enter a key and then your application will check okay does this exist on auto by the way here this should be replaced with key it's not going to be hard coded anymore well it's hardcoded at another point so to speak but this could be something that we get from user input or the user can select it from a ui or something like that and then the application would retrieve the data and display this instead of just in a console application in an actual ui and you will later see how to use uis or how to build uis using wpf so that is one way of getting around the problem another way would be to use something called try get value okay so let me quickly add a little bit of code and go over it so let's say we have this empty employee which i call result and it's empty because i set it to no and then i'm checking the employees directory if it has the value called inter and i'm using this try getvalue method so this try getvalue method will return a boolean which is great because that's what an if statement likes to have in the condition and it will print out the result and it will put the value into result so this result will actually be an employee so try get value we'll try to get the value that we pass here and if it worked the volume will return true so this if statement will be executed and at the same time it will get well put the result into this variable so the out will be put into this result variable so the value that we retrieve from our employees directory will be stored in result okay so that's what this statement here says and if it worked in fact and in turn does exist then this code will be executed and otherwise this code will be executed so this key does not exist so that would be just another way of getting around the same issue so try get value also works well so value retrieved employee name ernest intern 22 and he gets salary of 15 000 euros a year or whatever type of money you want to talk about here okay so now let's dig a little deep bit deeper because we can also get an element with the element at method so an element of our dictionary so as we learned or as you learned a dictionary is going to use structs here so a dictionary is a collection of key value pairs which is a struct that is defined like a key value pair that is going to look something like this here so let's look at it this here so we have the key and the value so this is the key value pair so that's what a dictionary is containing so let's look at this for a loop here and let me you can actually put it right here so this four loop what it will do is it will use this element at method however in order to use it we will need to implement a link so for now just accept that link is a powerful tool that allows us to work with collections in a very efficient way and we will have an extra chapter later on where we will look into links specifically but for now let's just accept it as that okay so once you get to the later chapters you will learn more about it and then this will make more sense okay so in our case in this particular example where we have our key value pair with the key and value where t key is the type of our keys of our dictionary and the same goes for the t value which is the value of our dictionary now in our case it's string and employee if we look at it okay so for our dictionary that we created here let's string an employee but internally behind this string key there is also an integer value so a number so to speak okay so let's say at position zero will be the ceo at position one the manager at position two the hr person at position three the secretary and so forth okay even though they will all be at the position so at the key of their role but internally they will also be at a number so the key will also be a number internally so that's exactly what we can use for this element at so this element add if you hover over it returns the element at a specified index in a sequence okay so a dictionary is a sequence so it will return basically our amplitude position 0 our employee at position 1 our amplitude position 2 and so forth however this will return a key value pair okay and the key value pair that we are going to create here is going to be of type string and employee so now we can display the key and then we can get the value of our employee at our key value pair at the value position because we get it from our element at okay this is a little more complicated but basically it is just another way to access an item inside of your dictionary not using the key as a string but the key as an integer and this goes for all different kinds of keys so no matter what type you used for the key there's always a number assigned to it as well so even if this was of type object or if this was of type double or whatever there is always an integer assigned to it which is allowing us to use this element at i okay so let's go through this and you will see that it will basically allow us to display all employees so it goes through all employees in our list so i believe it goes all the way up to here okay so it shows us all of the employees that we have in there and it even starts a little further up so this is basically the list that we are getting from this for loop okay so you can use a for loop to get through an entire list of dictionaries you can of course use it for each loop as well but you can also access items using element at i and then you can store it in an empty object because the value is going to be in fact of type employee because that's what we defined here now if you look at the key let's run it again you can see here key ceo key manager key hr key secretary now we could also print the i if you wanted to so here like so print well actually let's just print here i is and then i will be the second entry like so and then we print i okay so then you can directly see which integer value is assigned to a specific position so the ceo is at position zero the manager is at position one the hr person is at position two and so forth okay all right so that's it for this video i think it's a long enough video to go over dictionaries at least at this point so in the next video we're going to look at how we can update an existing dictionary so how we can update the value of a dictionary and then also see how we can remove data from a dictionary quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below welcome back in the last video we looked at dictionaries and we learned how to create dictionaries our assigned values dictionaries how to iterate through dictionaries we saw how to use this element i in order to get the id so to speak of a dictionary key item or of each item and now we're going to look at how we can update and remove data from a dictionary so it's actually quite trivial so let's say we have the following situation we want to update a specific key so we want to update a position at a specific key and that will be a little further down here so here we have the hr key that is the one that we want to update so we can check if there is in fact an employee in the directory that contains this key okay so we check if this key to update in this case hr exists and if it exists then we want to execute some code and what we want to do is we want to replace lora with a different employee so now lora will be replaced with a new employee called in order to update an array we can just say the dictionary name then the key name where we want to update something and then assigning the value that we want to put into its place so at this position that is going to be the key hr we are going to put this new employee object so we're going to say now instead of having the old person in hr which was laura we're going to replace her with the younger eleka okay so that's what this code will do for us so that's how you can update an entry basically dictionary name key name in squared brackets and then whatever you want to assign to it now the value that you're assigning to it has to of course follow the same type that the dictionary was defined with so in our case it has to be of type employee and this is in fact of type employee so now we can of course also print that into our console like so employee with role key 0 was updated at the position key to update okay so in this case hr was updated and then if that didn't work we can of course also print an error message here saying okay sorry the employee not found with this key and like so and i have one too many i think like so this will fix the error okay so let's run this real quick to see how we can update it and you see here at the very top i employ you with a role key hr was updated now let's look at the hr person where is she she's here it's aleca age 26 with a salary of 35 000 roughly okay so the update worked and we can see in our list of all update or all employees because that's where we got all employees from so now let's say we want to remove an item from our dictionary okay because here this is the for each loop that we used to add all of the items all of the employees to our dictionary then we edited one so this is where we update and now let's look at how we can remove and in order to remove we also need to have a string that will be the key that we want to replace because well we have a key value pair in our case that the key is a string and that's what we use here and then we can also check if that actually works so the thing is the remove method if you look at it it actually has or it returns a boolean so hovering over remove you will see it returns a boolean which means that it either removed it or it didn't remove anything so if it removed something what we're going to do is we're just going to say that at the key in this case intern the person was removed maybe their internship was over let's say after six months or whatever now we can delete that intern from our database okay so let's run this again in our case not database but dictionary okay so here at the top you see intern was removed and now let's see if we can find an intern and i believe we don't have an intern in our database here okay so we have the ceo the hr person the secretary as well as the lead developer and now of course if it didn't work we can have this else statement so if no then print an error message saying no employees found with this key and the key that we are looking at is the key to remove which will be our intern key which is basically this one here so i hope you recall that we used the role of the person as their key in the company so once again this example is really just to demonstrate the features of c-sharp that you have here this is not how you would write this so i would not recommend to use the role as the key for your data structure because the role is usually something that many people can have so you can have multiple people having the role of intern you can have multiple lead developers in a company or multiple junior developers and so forth so that's something you have to be super wary about now i would always recommend to use an id as the key okay so something like an integer where you assign an id to every single employee but this was just to show you that you have a lot of flexibility you can use strings as your keys you can use integers as your keys and so forth all right that's it for this video i hope you enjoyed it now you know how to create a dictionary on how to use dictionaries and now you can create a dictionary so that i can read one punch man and finally finish this book because it has been laying around there for a while actually i could probably finish it now already but still it's always good to keep on going and improving and learning new stuff and dictionaries is definitely something that you will need throughout your career as a c-sharp developer i hope you enjoyed this video and if you did then please hit the like button and the subscribe button that really helps us out and it will help you out in the future as well once we upload even more cool videos every single week there are a bunch of new videos coming up and also if you loved our content or loved our content in general check out the link in the description to get the c-sharp master class with a high discount so it's definitely going to help you out to become an even better developer so if you love the videos that we create and check out the course and now check out one of the other two videos and yeah thanks a lot for watching the video again
Info
Channel: tutorialsEU
Views: 6,298
Rating: undefined out of 5
Keywords: Tutorials, Programming, Course, Learn, Step by step, development, programmer, video course, video tutorial, learn how to, #funcoding, c# dictionary, C# dictionaries, indexes in c# dictionary, Fetching Data from a C# Dictionary, Adding key-value Pairs to a C# Dictionary, what is c# dictionary, learn to use c# dictionary, generic version of C# hashtable, c# projects, c# programming tutorials beginners, c# programming language, denis panjuta, denis panjuta c#, denis panjuta udemy
Id: jOLFsT6j9Wg
Channel Id: undefined
Length: 27min 25sec (1645 seconds)
Published: Tue Apr 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.