BrandGhost

C# Arrays, Lists, and Dictionaries (Quick dotnet tutorial)

This video is a brief overview for beginners on how you can effectively use arrays, lists, and dictionaries in your C# dotnet applications. I skip over the nitty-gritty details of why each data structure has different performance characteristics or how they function exactly under the hood, but instead, focus on how you can leverage them. If you want more in-depth explanations, let's hear about it in the comments! For more videos on programming with detailed examples, check this out: https://www...
View Transcript
So today we're going to look at arrays lists and dictionaries in C sharp there's already a ton of YouTube videos that exist that'll explain the performance characteristics of arrays how lists work how dictionaries work all of the details and especially going into a lot of granular detail that I'm going to be actually skipping over today because I just want to give you a brief review for how you can use these things in some of your earlier programs so this is very much going to be focused for beginners and if you're a more advanced programmer looking to try and tune your algorithms and understanding the different performance characteristics of these things this might be a little bit too introductory for you so just a heads up that we're just going to be focusing on the primary use case for these collections alright so out of our three collections we're going to be starting with arrays and in C sharp you have an array declared by using this syntax here with these square brackets so just to show you how it looks if you wanted an array of integers you would actually have int with these square brackets this is going to be the type so this array would be of type integer but the square brackets make it an array and you would give it a name like my array and the autocomplete here is kind of showing you some of the other syntax so you'll notice that on the right hand side of the equal sign we see new int with the square brackets again but in this case there's now a number on the inside one of the interesting properties about arrays and this is going to be something you'll want to consider if you're using arrays is that arrays or fixed size when we're talking about c-sharp and using arrays primarily your use case is going to be only when you have a fixed size set of data that's why when we're looking at the Declaration of an array we actually just want to be looking here where this one is and that's going to tell us the size of the array if we wanted an array of one integer which would be a little bit weird because it would just hold one integer you might as well just make it an integer instead of an array then you would use one but you could have an array of 10 integers you could have an array of 100 integers so on and so forth of course negative sizes do not make sense when you're declaring an array you can see that Visual Studio is highlighting this as an error it doesn't make any sense because the size cannot be less than zero so similarly if we wanted to have an array of strings instead of integers we could instead just change the type here to be string and we use the square bracket still and you'll see that this syntax now shows us that we have a string array and we are creating a new instance of the string array that is going to be 10 so it will hold 10 different strings inside of it let's walk through a super simple example where we're just going to store the days of the week inside of an array because there's seven days in the week we would just make this array of size 7 like I have here but now we have to look at actually allocating items into this array if you see the autocomplete syntax that's come up on my screen you can see that it's already trying to suggest to us that we have something like my array with the square brackets and then a 0 inside of that equals and then it's empty so if I press tab here to have this autocomplete filled out what this syntax is actually showing us is that we are indexing into the array at the zeroth element so that would just mean the first element and then we are going to be able to assign something to this array and because it's an array of strings we would be able to put a string here at the first element so I'm going to go ahead and put Monday here we can go ahead and repeat this and this autocomplete is great it's like it knows what I'm trying to do already and I can put Tuesday and so on and so forth all right now that I have all seven days of the week added into this array just a quick sanity check here we can see that we start at zero as the first element of the array and we're finishing at six and if you think about it I said seven days in the week but since we're starting at zero we should actually finish off at index six for Sunday so this demonstrates how we assign elements into the array and in practice this same syntax that we're using here on the left hand side that has this indexing with the square brackets that's exactly what we would want to use to be able to read elements from the array so in C sharp if I wanted to be able to print out the first day of the week according to our array I would just simply use my array use the same indexing that we saw right up here where we assigned Monday but instead of assigning anything I'm just using the indexer to pull the value out that will mean that the value that comes out of index 0 is going to be passed into right line if we actually wanted to print out all of the items in the array and get all of the days of the week printed we could just use a for Loop and just to briefly explain the syntax here we have a number that would start at zero it would run this Loop until the number I is less than the length and this is important to consider because we can't index at position 7 for example right because there's only seven slots in the array and the final index we can use is six so that would mean if this Loop allowed us to go where I was actually equal to the length we would go out of the bounds of the array so it might look a little bit funny but we actually want to stop the looping before we get to the end so that means that I has to be less than the length and then the last part here is just that we increment I by One for each iteration of the loop and if I copy actually I will just cut this line and put it here I can pass in I as the index and if I go ahead and run this what we would expect to see is that we would start at index 0 passed into my array here and that would give us Monday and then the next iteration of the loop because I is going up by 1 we would then get Tuesday all the way up to Sunday so let's go ahead and run that so it's not super exciting but as you can see we have all the days of the week printed right here from our array and if we go look at the array assignment we can see that it lines up perfectly so just a quick recap on when and why you would want to use arrays well if you have a collection of things and it's going to be a fixed size you can absolutely use an array for that you're able to directly index at each position in the array to either assign things to it or read elements from the array at those indices arrays are not going to be great for you if you want to have a dynamically sized collection so I would highly recommend to not use an array for that and without getting into the performance details in depth indexing into an array like this and being able to read the elements back out as well are both extremely fast operations so an array is designed for you to be able to do this very efficiently the next collection that we're going to look at is the list so declaring a list in C sharp is very similar to what declaring the array looked like but we're not going to be using square brackets instead if we wanted a list of strings the autocomplete is going to show you directly on my screen right now and I'll just press tab to have that filled out and I'll explain what's going on here so a list is a collection then in C sharp is known as a generic so if you're not familiar with generics and I assume you may not be if you're watching this video as it's really meant for beginners generics just allow you to have classes that Implement something like some type of algorithm that does not actually depend on the data type so for example an implementation of a list in C sharp using strings as the elements of the list is actually the same implementation as you would have for a list of integers or other objects in this way the as the name suggests you could have a generic list and then because C sharp is strongly typed we can just say what type of list we want to have so there's lots of information about generics in other YouTube videos I'm not going to get into the details of that but hopefully you can understand that this is going to indicate that we want a list of strings and for example if I just wanted to change it to be a list of integers that's the only thing I have to change on the right hand side just like we had for the array we're going to create a new instance of the list and what you'll notice is that I'm not doing anything with a size here technically a list does have a capacity that you can set like an initial size but one of the benefits of lists are that they dynamically resize for you as you're adding elements so I'm not going to dive into the details about the performance characteristics of adding things to a list and how that resizes but this is handled for you in C sharp under the hood so that you don't have to think about changing the size of the list it will just happen for you so to compare lists to arrays if I scroll up a little bit and look at our other example we could in Theory actually just take this code paste it down below and then if I change the variable from my array to the list the code actually compiles however we'd want to be careful about this because our list when it starts off like this is of size zero and that means that if I tried to assign something to the zeroth position so the first position of the list this would actually throw an exception for us and even if I didn't have this one and I wanted to go look at index one and assign something there again we would run into a similar issue where we would be outside of the bounds of the list so just because this compiles does not mean that this will actually run and execute properly so if we wanted to assign all of the days of the week to a list there's an easy way that we could do that and there's really just a method that we can use called add and if we go and add all of these items into the list behind the scenes this list will be changing size for us and being able to accommodate each of these items that we're adding into it afterwards we could actually go index into this list just like I was showing you and actually get those items back out at the indices that we saw Above So to quickly illustrate that I'm going to go ahead and copy and paste this Loop and then I'm going to change the variable from my array to my list now one difference is that arrays use the property called length to give you back the count or the size of the array whereas list actually uses count as the property this is going to be the exact same thing it's just that there's a different property name to pull back the same meaning for the different classes we're looking at so if I go ahead and run this now we should actually see the days of the week printed out twice because I left in this original Loop and if we do a quick scan through here you can see that we have Monday through Sunday then Monday through Sunday once again and just to demonstrate that we can use indices on lists just like we did for arrays for both assigning things and reading from them what I'm going to do is show in this Loop that we want to actually take the first three letters of everything that's been added and reassign it back to the position that we're looking at so this part right here is just going to give us the first three characters of each string but the important part that I want to show you is that the left hand side shows us which index we're going to be assigning into the list and the right hand side right here is actually which index of the list we're going to be reading from what we should end up seeing is that the first element is actually just going to say m-o-n the second one will say t-u-e and so on and so forth because we're actually taking the exact same index and then taking the string from there and just taking the first three characters and then reassigning it right back into the same spot so let's go ahead and run that and before I forget I need to have this console right line in there so we can actually see the values get printed out and awesome as we probably expected to see we still have the first days of the week from the array printed above and now we can see at the very end we have mon through sun because we took the first three characters of everything in the list so just to quickly recap this syntax allowed us to read out an element and then we could assign it back into the same spot and above we use the add method to actually change the size of the list as we were adding elements without going through an in-depth example if we use the remove method we can actually remove thing things from the list the same way that we added things and it would also dynamically change the size so if you're trying to make decisions about whether you should use an array or a list most of the time especially as a beginner using something like a list is probably pretty safe for you to use and this is because it has a lot of the same functionality that an array has except you can dynamically change the size of your collection if you do in fact know that you're going to be dealing with a fixed size collection then array can be great because it's just going to remain fixed size and overall has a little bit less overhead than using a list the final collection that we're going to look at is called a dictionary if you've been learning about programming you've probably come across the terms dictionary hash map hash table and what we're going to be looking at today is c-sharp's dictionary which is going to be the data structure that represents this the Syntax for a dictionary in C sharp is going to look a little bit more confusing than what we've seen so far with arrays and lists but it's pretty easy to explain alright so what I put on the screen screen for us to look at is going to be a dictionary that is keyed by strings and has values that are of type integer so just to repeat the Syntax for the dictionary type is going to be that the first type we pass in is going to be the key and the second is the value and if you recall when we were looking at lists we only had one generic type that we were passing in and that's because we were simply looking at the values of each item in the list but with a dictionary each entry in the dictionary is going to have a key and a value and that's why we need two different types that we can look at you could of course change this to be other types if you wanted to so if you wanted to be keyed by integers but have strings as the values you'd simply swap these around if we think about the use case that we might want to use a dictionary it's actually very similar to a list and that we're able to have something that dynamically changes size and we're able to index into it to assign elements and get elements out of it however one of the fundamental differences in usability for a dictionary is that instead of indexing at a particular location into the dictionary like we would with an array or a list where we have to give it a numeric position the index that we give it is actually based on the type that we have here as the first parameter so in this case we'd use a string inside of our square brackets to index instead of a number we could go ahead and swap the key and value Types on this dictionary and actually make it look very similar to the examples that we saw above I'm just going to copy and paste the array code that we used right in the beginning and then change all of the My array variables here to be dictionary you'll notice that right away there's no actual error so this code should in fact compile and unlike the list example where I showed you that this would also work for compilation this will actually work at runtime as well the reason that this works is that we're able to actually assign the right hand side into the position of the dictionary and this will dynamically resize the dictionary for us to include this element so we could quite literally take the example from above with the list paste it below here and then take the dictionary variable and put it in place of all of the list variables that we see here and the only reason I'm showing you this is to demonstrate that we have things like account on the dictionary we have the ability to assign to an index of the dictionary and then we're able to read from an index of the dictionary as well the dictionary type also has add and remove so if I go to add something here you can see that I need an integer key and a string value so for example if I comment outline 37 and have line 38 here these are functionally equivalent the one difference would be that when we are doing this direct assignment to an index we will always overwrite what's at that index or add something if it doesn't exist when we use this syntax here to add something into the dictionary if this key already exists and we try to add it will throw an exception so if you always want overwriting Behavior you can just simply use this index mixing notation however if you want to add things and have some safety that your program will stop and throw an exception if there's a duplicate key then using this syntax with ADD might be something that you prefer so if I go ahead and run this you'll see that I have the three letter days of the week printed out twice once again and that's because this last set here that I have highlighted actually comes from the dictionary example so a quick recap on dictionaries here if you want to have a collection that you can index into and you don't necessarily want to use integers to index at a particular position a dictionary is super helpful for that the first type that we have on the generic dictionary declaration here is the type of the key and the second type is the type of the value in this case I used integer and string so that it would end up looking like our previous examples but in fact like I said if you don't want to actually index with integers you can absolutely change this first type to be the type of the key that you want to have and then use that here dictionaries like lists also have this add and remove syntax and like arrays and lists allow us to index into them with this square bracket syntax so that's just a super quick one for today and hopefully if you're just getting started in programming and looking at arrays lists and dictionaries this helps a little bit in C sharp we were able to look at the array type as well as the generic list and generic dictionary type and then look at the similarities and differences between them so I had mentioned that arrays are useful if you have fixed size sets of data and that's because resizing an array is not a straightforward operation so if you're looking to have something like an array but you want resizing capabilities then a list is going to be a very good choice instead we looked at a dictionary last and it actually has a lot of similar characteristics to a list but instead of just indexing in at a particular position inside of that collection what we're able to do is index with a particular key which does not have to be a number and represent a physical position within the collection we're able to use that key to get a hash value and then that dictionary can look up the value for that hash like lists dictionaries are also dynamically sized so again that can be really useful if you know that your data set size might be changing if you thought that was useful please give the video a thumbs up subscribe to the channel for more basic introductory programming Concepts as well as software engineering Concepts and more advanced c-sharp topics thanks and we'll see you next time

Frequently Asked Questions

What is the main difference between arrays and lists in C#?

The main difference is that arrays have a fixed size, meaning you need to specify the number of elements they will hold when you create them. In contrast, lists are dynamic and can resize automatically as you add or remove elements.

How do I access elements in an array or a list?

You can access elements in both arrays and lists using an index. For example, if you have an array called 'myArray', you would use 'myArray[0]' to access the first element. Similarly, for a list called 'myList', you would use 'myList[0]' to get the first item.

What is a dictionary in C# and how is it different from arrays and lists?

A dictionary in C# is a collection that stores key-value pairs, allowing you to access values using unique keys instead of numeric indices. This makes dictionaries particularly useful when you want to associate specific keys with values, unlike arrays and lists which use numeric indexing.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload