C-SHARP - 4.3 Lists

Hello everyone and welcome to this new video. In this video, we are going to talk about a very important notion which are lists. What is a list? A list is relatively similar to arrays except that you can do much more. Just like the array, we will be able to contain several elements but we will also be able to add elements as we go because the size of the list is not fixed. I'll show you how to make a list in C#. There are 2 ways. The first basic is to type List then you put <> tags and inside, you give the type of elements you want to store in the list. Here I will store strings so I put string between the tags. Then you give a name to this list. Now as for the array we will allocate memory with the new. We put back our list declaration and at the end unlike the array, we put parentheses and don't forget them; You see that the lists since the size is not fixed, we do not need to specify the number of elements we want to store in our list. You can store as many items as you want. And the 2nd way, it's more for a question of readability, instead of declaring a type list here we will replace it with var because you know that the type will be automatically chosen at the initialization done here Basically the type of the list will be deduced according to what we are going to put there. I go to the var type so that we have better readability after you can choose what you want. Now to add elements to our list, there is an Add() method which takes an element as a parameter and will add it to the list in which it points. So here if we want to add a name to our list. We type the name of our list, then we point to the Add() function and in the parentheses I give it the element that I want to add to my list, I will put the word Hello We can also do directly as for the arrays, after the parentheses here, we put braces and we add the elements that we want to store except that here we can add elements later unlike the arrays with the Add() function I will add 2 elements and to display our list, it is exactly the same as the arrays. We are going to make a for then between the parentheses we put an int i = 0; here the condition c is that as long as i is less than the number of elements in my list For arrays which allows us to retrieve the number of elements this was the .Length method. On the other hand, for lists it will be another method called Count. So we put a .Count and we finish by incrementing i. In the braces, we will put a Console.WriteLine() to display the elements of the list. And there we use the same syntax as the arrays with square brackets to access the element with respect to the index. Here I pass i to display all the elements of the list If I launch the program we have our list which is displayed. You have seen in our list here I have two elements and in the output window I have 3. Well that's d to the fact that here with the Add method, we added the element after the elements that are already in our list. You can also access an element as for arrays thanks to the index. For example here, I'm going to delete all that and I want to modify the 2nd element of my list. I make a list of hooks I recover the index of my element is 1 then I do = and I give the value I want which will replace my Hello I do a Console.WriteLine() before and after the modification so that you can see that the modification is carried out correctly. I run the program. And you see? Before the modification our 2 me element is equivalent to Hello and then after the modification it is equivalent to Bonjour. So if you want to modify an element in your list, you do the same way as I just did. Then there is another method which allows this time to delete an element from my list. And this method is the Remove() method. It takes the value of an element as a parameter. This method will remove the first occurrence of the value put in the parentheses of the remove. I explain, basically this method will browse the list, and the first element which corresponds to the element that we put in the parentheses it will delete it. If for example there are 2 times Hello in my list then it will delete the 1st but not the 2nd. I'm going to quickly do a little for to show you the elements of my list to test our Remove() function. I'm going to add a second hello and a word between the two just before launching to separate them, so that you can see that she's only going to delete one. I run the program and you see? The remove method has indeed deleted the first occurrence of the element that I have given it as a parameter. There is another Remove() method except that it takes the index of an element as a parameter. Basically, it will delete the element found at the index passed to it as a parameter. This method is RemoveAt() We will test, I will delete this line and I will type liste.RemoveAt parentheses and inside I will give the index of the element that I want to delete. I want to delete the 1st item from my list, so I'm going to give the index 0. I run the program and you see? The first item in my list has been deleted. So until now, we have done the same thing as with the tables, you will tell me but why do we have both the tables and the lists since it is almost the same. Well, in reality, it's not exactly the same, because we saw with the Add() method that we could add elements to our list without limit and if, for example, we create a program that asks the user to enter a multitude of name to create a football team, well we will do ReadLine() each time, either do Adds to add players in bulk to the items in the list without needing to know how many people there will be in the team in advance. You see here, I didn't need to give my list any size when declaring. And that's the first thing. And the 2 me, c is like the view we can delete elements in our list. After there are a lot of methods to act on the lists, we are not going to see them all in this video but we are going to do a little exercise this time to work with the lists. Here we are going to create a program that will allow a user to enter his entire indoor football team. So first of all, we will create a list that will store the names of the players in the team. We will then type var then we will give it the name liste_joueurs then = new List <> and between the tags the type of elements that we will store, these are first names or strings. And now, how are we going to ask the user to enter several names? First, to interact with the user, what we know is the Console.ReadLine() function, then you have to repeat it several times because there are several players so whoever says several times says we are going to use one loop. Then to add the players to my list, we now know the Add() function so normally it's good, we have everything to do our program. First, we will start with the simplest, we will ask the user to enter a name and we will add it to the list. For a we will do a Console.WriteLine() and we will display Enter the name of a player: Then we will create a variable of type string which will store the name of the player that the user will enter. So I type string I give it the name player = the function Console.ReadLine(); And to add it to the list, I retrieve the name of the list either liste_joueurs then .Add() to add an element to my list and between the brackets I put the element I want to inject either the name of the player so I put the player variable. Now I have to repeat these instructions several times because for the moment, it only works once. So we are going to introduce them in a loop so that we can repeat these instructions several times. This time to change, we will do a while. Now, there is a problem, we have to make a program that must adapt regardless of the situation, that is to say, we don't know in advance how many players there will be in the team, whether it's going to be do we put in the parentheses of the while to ensure that our loop adapts to any situation? First solution, it is to ask the user to enter the number of players in his team and to complete as long as i is less than the number of players. However, I do not want to proceed in this way because I want to work on your logic. Here I will start with an infinite loop, or the condition in the while will always be true and the loop will never stop. Are you going to tell me that if it turns to infinity the program will ask the user to enter names to infinity? I come to this, before we are going to put in the parentheses of the while the word true so that the condition is always true. Then in the Console.WriteLine I will add information to the user In quotes, after the colon I will put: parenthesis Otherwise type enter to finish Basically, what do I want to do here? It is that when the user will enter an empty string then the program will stop. To do this just after this line I am going to do an if and the condition c is that if the user enters an empty string either he types enter then we exit the loop. For a I type player == has an empty string since what the user enters is stored in player. Then in the braces I do like the switch you remember I do a break; to break the loop. And if not if among other things is the name of a player then it is now that I will inject into my list. So I recap, here I created a while loop with a condition that is always true, to allow having any number of players in your team. Then we ask the user to enter the name of a player if he types the name then we add it to the list with the add since it does not fit in the if because it is not equal to an empty string then we start the process again same thing until the user has finished entering his team. Once he was done, he typed enter directly as indicated in the Console. WriteLine () to finish so in the player variable we will store an empty string. Either we will enter the if condition and we execute the break statement which will allow us to exit the loop. Before testing, I'm just going to do a for loop to display the elements that will be stored in my list So I'm doing a for int i = 0; the condition is that as long as i is less than the number of elements that there will be in my list, either I type liste_joueurs.Count and I don't forget the increment of i. And to finish in the braces, to display the elements of my list I do a Console. WriteLine and in the brackets as for the tables I give the name of my list is liste_joueurs hooks and i as index to recover each element. Now let's run the program. He asks me to enter the name of a player I type Jason I enter, he asks me again for the name of a player I type Thibaut I enter Then I type Mohammed I enter. You see ? I can type the number of players I want the program adapts to any situation regardless of the number of players I enter. I can continue the loop will turn to infinity but I am going to stop there I am going to enter as indicated to leave. And hop you see? the loop stops. The user enters an empty string so we enter the if condition and the break allows us to exit the loop and then we display all the elements of our list with the for loop. So there you go, I think I've done the rounds for the lists, we'll practice and we'll use the lists in a more advanced way in future videos. Anyway, I hope you enjoyed this video and that everything was clear to you. See you next time in a new video.