C# Array vs Dictionary vs List a Beginners Guide in .NET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're a new developer you've probably asked yourself what is the difference between an array a dictionary and a list in this video i'm going to show you what the differences are and where you should use them hey everybody it's francisco with the dev live welcome back to the channel if you're new to the channel i'm a self-taught developer with more than 10 years of experience in this channel we make videos for new developers like yourself with intent for them to become resources you want to know about arrays dictionaries and lists let's find out what the differences are i've gone ahead and created a console application in visual studio and the only thing that i have added is a class called collections class i also created a class to hold an object for a user which will contain the user id the first name and last name of a user also in the main function for the console application i instantiated that class so the next step that we are going to do is i'm going to go ahead and define the constructor for this class all i'm going to be doing is that i'm going to be instantiating this private variables user 1 user 2 and user 3 and i'm going to be adding data to them so basically i will be creating the users in our application uh you can go ahead and skip i'm going to add chapters to this video so if you think this is boring you could just fast forward to when we start talking about arrays and how to use them okay so let's go ahead and define our our constructor the only thing we are going to be doing here is that for user one we are going to be creating a new user class [Music] and we are going to be setting the values for this user so for user id we are going to go ahead and say this is going to be user one for first name we are going to use um let's do johnny and for last name we are going to do appleseed uh for user two we are going to be doing something similar to save time i'm just going to go ahead and copy this same code and we'll just change the the property values for each one of these so this is going to be user 2 and this is going to be [Music] this is going to be john oh i just realized i misspelled up here so it should be spelled like that so first name should be john last name will be do for the third user user id will be three first name will be paul and last name let me see if you can guess the last name yeah you're right bunny so now we have a constructor that will actually generate this this three user classes or models for us okay so now to get to the fun part we are going to create a function and let's just call it a raise function so we'll know that in this function we'll be working with arrays it's not going to return anything so therefore we're going to say it's going to be a void class and to use it from the from the console up what we have to do is say a collections.erase function and just to keep the console application open let's do console redline and this is going to have the application wait until we have some sort of input in the application how do you instantiate an array or how do you put together an array so obviously if you're watching these videos because you are looking for a way to work with collections of data right it could be a collection of primary data types so it could be something a little bit more complex like this user class i think that you have the need to work with a collection of either variables or objects so how do we instantiate or create an array in this case we want to create an array that's going to hold three elements or three objects of the type user for the class that we just created earlier let's go ahead and and do that so this is how you'll do it so you're going to have a variable you could either do variable or you could do user array and then you give the name of the array i'm going to call it users this is going to instantiate a new array of type user with three elements in it to begin storing the user profiles we created we do it this way so we say that users at location 0 which is the first location or index of the array let's say that we want to hold user 1 and then of users of 1 we are going to hold user 2 and users of 2 will hold user 3. ok so now we have data in our array how do we iterate through it the most popular way of doing that is to use a for loop so let's go ahead and set a for loop for this so we can iterate through the different elements uh so we're going to set this up and we are going to iterate based on the variable i and while the variable is less than three we are going to keep iterating and we are going to add one to the value of i on each iteration let's go ahead and print the value of each of the elements in the array as we loop through them so to do that let's go ahead and pull the variable out for the current user from the array so at the location of i as we're iterating we are going to pull the value of it so now this is going to be the user and then we are going to print that out to the to the output screen of the console application so here we are going to do user and we are going to print out the value user that first name actually let's do the user id first and then a space and let's do the user.firstname and the last name okay and as i'm getting ready to run the application i notice that i made a mistake up here so here i forgot to change the user variable so i was setting pretty much overwriting user one um each time that i was going through this user one user two okay okay so i think this should work let's give it a try let's try running the application now and see what we have so far as you can see we have all three users this i think is a good way for us to store data but what happens if you want to search and find a user let's say by the id or by the first name even if it's just by the id in this case you're gonna have to go through the whole array or at least a big part of it searching through the elements one by one until you find a match based on the on the user id arrays are simple you can get them started you know pretty quickly but they're not very useful other than to just iterate through them and do something with the elements or with the data that you have stored in in an array so let's go to dictionaries now these are similar but not exactly the same so let me show you what i mean by that okay so now let's leave this function the way it is so we can reference it back in in later in the video for this uh let's go ahead and create a function and let's just follow the same pattern let's call this the dictionary function okay so in this one the first thing we want to do is create a new dictionary and again we're going to call this users as well and this is going to be a new dictionary and the key is going to be an integer and the actual data object will store or the value in this case will be a user okay so now we have a dictionary now how do we add elements to the dictionary how do we populate it if we reference a function that we created for arrays is going to be somewhat similar so we are going to do [Music] users users we are going to add a new element to that but we have to provide a key so the key for the first user will add will be the user id and the value that we store for this element will be user one and we are going to do the same thing for all three users but this time we're going to remember to change all the variables so this is going to be user 2 and we are going to be storing user 2 at this location then we're going to have user 3 and then user3 as well now that we have populated the dictionary we could also iterate through it so we could do something like this so for each for each variable user in in the dictionary we are going to print out we are going to do something similar to what we did in the arrays function so we are going to go ahead and again write write a line in the console and we're going to get the user and we are going to print the key and from the value we're going to grab the first name and we are also going to grab the last name okay so let's go ahead now and run this function so instead of calling the collections arrays function let's comment that line out and now let's invoke the dictionary function so this should give us something very similar and yes indeed very similar to the output that we got with an array now this is very simple right now we just iterated when each element at a time and printed out the data that we're holding on each of the elements okay so now the next exercise we're going to do is we are going to pull one of the data elements out based on the user id and this is the way that we do that so now we don't have to iterate through the whole dictionary right because your dictionary could contain 100 elements a thousand elements or you know a bunch of them so you don't want to take the time to loop through all of them to find your match value like you would do with an array using a dictionary gives you the ability to go directly to the item that you're looking for and retrieve it and this is the way we do it so we have to declare a new variable of type user the first thing we have to do is declare a variable of type user and let's set it to null we are going to catch the result and let's try to retrieve the object from the user and for that we use the try get value we will pass the the value of the key that we use to store the item in this case we said we're interested in retrieving john doe which is user id number two and we're going to tell this function or method to store the value if it's found on the variable that we just created then we are going to our condition here that if the if a match was found let's go ahead and print it out just like we did before um when we iterated through it let's go ahead and do this uh this is going to be slightly different so this is going to be user id we don't need to reference a value here nor here okay let's run this and i'm going to put a breakpoint here [Music] okay so as you can see this executed and it's telling us that it in fact did find a match so if we let the application continue we see that we did print out the value john doe which is a match that we that we got now what happens if we look for a user id that does not exist so let's say that we try to retrieve user id 1000. let's go ahead and execute that and you'll see that the application didn't find a match for user id 1000 therefore we don't print anything and as you can see nothing was printed out in it and the application's still waiting for me to do some type of input [Music] so now we we went through a race we went through dictionaries and so far i'm liking dictionaries more than i'm liking a race at least for the flexibility right so whatever an array does a dictionary can do and a little more right so now let's go to lists and now you're gonna find out why i prefer to use lists whenever i can same thing as we've done before now it's going to be a list class or list functions excuse me so we're going to be doing something similar to what we've done here we are going to be creating a list and that this list is going to hold users or the user type we're going to call it the same name and we'll instantiate it now the way that we add elements to this list or to any list for that matter it's similar to a dictionary with the difference that you don't have to specify a key you simply add one item after the other and you don't have to worry about specifying a key now let's do the same exercise we've done with the other two uh collection types right so let's iterate through it how do you iterate through a list of objects very easy so this case for each var user and users and we're going to do the same thing we've been doing we are going to print out the users and the list and that's it that's that's all you have to do so pretty much you loop through them one by one and then you pull the values out so let's go ahead and run this well actually there's a small change i have to do we have to change the function that we are calling here and now it's going to be the list function all right now we can run the application as you can see just like we did with the other ones we have the same the same output we're outputting all of the users in the in the list what if you want to find the second user in the list just like we did with the dictionary by using the user id value so let me comment this out and i'm going to show you how you can search through your list so you want to return the user who contains the user id with the value of two and this is what you do one line one line of code and then let's output it and there you have it so now now we retrieve the second user just like we did with the dictionary another cool thing with list is that you're not limited to be able to search only by the key or the user id in this case like we did in the dictionary with a list you can search pretty much by any of the properties that you have in your data object so for example let's say that you want to retrieve all of the users that the first name let's say that you want to retrieve the user whose first name is paul let me show you how you would do that there could be multiple matches in your data set so we have to assume that they could either be one or more results or none for that matter but it could be more than one so we have to set it up that way so we're going to create a variable called matched match users and we are like i said before we are going to retrieve all of the usernames or all of the users with the first name poll so this is how we would do that and since comparisons are case sensitive we have to make sure that we either convert to lower or to upper to make sure that we find a proper match in my case i'm going to use lowercase and i'm just going to say paul and whatever matches we find we are going to retrieve them or return them as a list and then we're going to look through the results and we are going to print them out so we already have code for doing that i'm a lazy developer or i'm a lazy programmer so i'm not going to retype everything i'm just going to rename this variable here and that should do what we need the application to do in this case so let's go ahead and run the application and see what we get okay as we expected out of the three users there's only one user whose first name is paul and there you see the output all another type of search that you can do is you can search to retrieve any names that for example contain as letters or begin with the letter so let's do a search for users who contain the letters j o in their first name we already have some of it all already laid out and the only change that we need to do is here so here we are going to again convert the first name to lowercase and we are going to say that we want the first names that contain the letters j o so this in theory should return johnny apple seed and should also return john doe let's go ahead and run the application and there you have it in conclusion lists are my favorite collection to use i tend to gravitate to using in lists and the reason is because sometimes i start using an array or a dictionary and later when the application matures more i find out that i have the need to search by another property value or search through or iterate through the list to return different values so in in my opinion i think most of the time you will be saved using a list and at the same time you will be having all these possibilities that you can do with with lists right that you don't have with the dictionary or an array thank you for sticking around until the end of the video i would really appreciate it if you gave us a like it would really help the channel out and don't forget to subscribe catch in the next one [Music] are we done was that it was that good you think yeah i hope it was okay i think it was good yeah they'll subscribe you don't think so now they will you will right you will yeah okay bye
Info
Channel: thedevlife
Views: 960
Rating: undefined out of 5
Keywords: c# tutorial, c sharp, how to learn c#, c# (programming language), array vs dictionary vs list, array vs dictionary, array and list in c#, array vs list, dictionary vs list c#, dictionary vs list, arrays vs dictionaries vs lists, arrays and lists in c#, arrays vs lists, arrays vs dictionaries, collections, collections in c#, collections in c# tutorial, collections basics, collection basics in c#, for loop, for loop c#, foreach c#, foreach loop c#, collections tutorial in c#
Id: raDvOfvc2Ro
Channel Id: undefined
Length: 20min 48sec (1248 seconds)
Published: Wed Jul 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.