Learn How To Program In C# Part 21 – Lists


Hey, everybody. Today we will be learning about lists. We’ve already learned about arrays. But arrays are a bit limited to their size. If you declare them to be of size 5, which
is the example that we have here, int[] array=new int[5];
array[0]=1; array[1]=10;
array[2]=100; array[3]=1000;
array[4]=10000; then you can only put up to 5 elements inside
of the array. But what if we want to have a variable-sized
collection instead of a fixed-sized collection like an array is? Then we would consider using lists. That’s where lists come in. An array is a fixed-size collection, but a
list is a variable-sized collection. One more thing, I want to show you how to
initialize your array in a line instead of doing the way we did it previously. int[] array=new int[5] {1, 10, 100, 1000,
10000}; This is how you do it in a line, much cleaner. But now I will create a list. Listlist=new List() {1, 10, 100,
1000, 10000}; We already know that int is a type. We know the format of declaring a new variable. So the type of the array we put here, but
the type of a list we put inside angle brackets. This list is a list of integers, and this
is where we initialize it. And we have the new reserved keyword as well. A list under the hood stores its items inside
of an array. Internally, it has an array and it uses that
array to store all the elements there. What happens when you put enough elements
and it’s over the size of the array? At that point, a new array is created and
all of the previous elements get copied over. And it continues on adding to the internal
array. That’s one good thing to know about lists. We can add to our list. List.Add(1);
List.Add(1000); These statements add the values 1 and 1000
at the indices 5 and 6 of the list. And we can remove elements. If we want to
remove the element at index 5, List.RemoveAt(5);
Now we run this program. Let’s hover over the array and see what’s
inside. Then let’s step over that. Now let’s hover over the list. We see the newly added 1 at index 5 in the
list. And then 1000 gets added in the list. You see the index on the left side. This is the index. And we are going to be removing an element
at index 5. Let’s check it. After I run through the line that removes
the item, it should be 1, 10, 100, 1000, 10000, and then 1000 again. The element at index 5 was removed. This is pretty cool because we are not limited
to the size. And we can keep adding and removing elements
if we need to. These are two different tools to be used in
appropriate scenarios. And it’s good to know about lists because
they are very useful. You can add and remove even after you have
initialized and put values inside. One more thing, I’m pretty sure you’ve
already guessed, but we don’t have to initialize it. I just did it so I could compare it with the
array that we had from the previous examples. We definitely don’t have to do that. Take that part out. And now you have an empty list. Listlist=new List();
List.Add(1); List.Add(1000); List.RemoveAt(5);
If we run this code, it will only have 1 and 1000, and the last line will not work. I could instead say something like:
List.RemoveAt(1); That would remove 1000 at index 1 because
1 is at index 0 and 1000 is at index 1. That’s it. If you’ve liked this video, please give
it a thumbs up. And please subscribe if you haven’t yet
so that I can keep updating you with the latest content.

3 Replies to “Learn How To Program In C# Part 21 – Lists”

  1. Is there more to know about list? I'm looking on stackoverflow and people are posting code that compares two list and I have no idea what is going on. X_x

Leave a Reply

Your email address will not be published. Required fields are marked *