C-SHARP - 4.4 ArrayList
Hello everyone and welcome to this new video. In this video, we will see a new concept called ArrayList. Until now we have seen that for arrays as well as lists when we declare them we could only store elements of the same type that we had specified during initialization. That is to say that we can create a list of type string and we can only store strings inside this list and nothing else. However in C#, there are ArrayList, it looks very similar to arrays and lists but in reality they are lists which allow to store several elements of different types. We will see immediately how to do it, so you type ArrayList then you give it a name for example lists = new ArrayList() parenth ses; And you see, it doesn't recognize it, it's underlined in red. You click here and you will have to add using system.collection. Indeed, this class contains interfaces and classes including the ArrayList class that we need And to add elements in our ArrayList Either you do with the Add() method As for lists. Either just after the parentheses you put braces with your elements. I will use the 2 as you see the different ways of doing it. I make braces and there I can put elements of the type I want. I'm going to put a number, then a boolean I put true, then a string Unlike the list, I didn't need to specify the type I want to store in my ArrayList. This is called an object that is not strictly type. Contrary to the list or an array which are strictly type, they are given a reference type. Here as you can see I put an int a boolean. I can really mix everything inside. And now if I want to display the elements of my Arraylist, I do as for lists. I loop for int i = 0 i strictly less than list.Count and i++. And i will do a Console.WriteLine() and inside i list. And I run the program, you see the elements of the ArrayList is displayed well. But hey, what is the ArrayList used for? To be sincere with you, it is very little used. Simply I show it to you because it remains a notion to know in case? or you will be working in your future projects and you come across an ArrayList. But in our next lessons we will use it very little if ever Why? Because mixing types is not a good practice in programming. The ArrayList will be used in very special cases but in general avoid using it because mixing types is often a source of confusion and bugs Difficult to solve or a lot of waste of time ! I'm just going to dig a little deeper so that you know how to use them well. Now imagine that I want to store this age in a variable. So you're going to do int age = list[0] right? However you see that it is underlined. It informs us that it is impossible to implicitly convert the type object to int because since we can store any type, in fact each time you access an element you will retrieve an object type element. And this object type can be any type. Since we know that the first element c is an int type, we can force it to convert to an int type. We are allowed to do this in C#. To do this, in front of the list we will put parentheses and the type Then display My age is: + age, and now if I run the program. I have: My age is 18. On the other hand, I was not obliged to force the type here. In C#, as basically calls casting a conversion. I could have directly put the list of zero in the Console. WriteLine() or change the type by putting object or var, it will automatically deduce the type object. But if for example I want to modify the age and I do age = list of zero + 1. What will happen? He tells me here that it is impossible to apply the operator + to an operand of type object. Am I obliged here to change the type object to type int or var to perform this operation. And you see that here we are limited with the object type, we do not have access to all the possibilities of the types. So if I really want to make a modification on an int I have to force the conversion. And you will see that it will work. If I restart the program. You see that we have displayed 19. Earlier, I told you that it was dangerous to mix types and to use ArrayLists. Bah you see if you are wrong in your indexes. And that finally instead of recovering an int you type here 1 instead of 0 and therefore you recover a boolean. If I run the program, you see that the program compiles but it throws me an exception because it makes sense, it is not able to convert a boolean to an int. So what I was saying about the ArrayList, that everything is going to be fine but when running the program and well without a doubt, you are going to have bugs because no matter what instruction you are going to do, it may not be not suitable for the right types. And the program will not work. For many developers, it is preferable to work with collections with strong typing, whether the type is specified from the start, such as arrays or lists, or still others such as dictionaries, which we will see in future courses. is that the ArrayList. Anyway, that's all for this video. I hope you liked this video and that everything was clear for you. See you next time in a new video. it is preferable to work with collections with strong typing, whether the type is specified from the start, such as arrays or lists, or still others such as dictionaries, which we will see in future courses, rather than the type. ArrayList. Anyway, that's all for this video. I hope you liked this video and that everything was clear for you. See you next time in a new video. it is preferable to work with collections with strong typing, whether the type is specified from the start, such as arrays or lists, or still others such as dictionaries, which we will see in future courses, rather than the type. ArrayList. Anyway, that's all for this video. I hope you liked this video and that everything was clear for you. See you next time in a new video.