This avoids the type safety issues and the performance issues of packing and unpacking mentioned earlier. In the above example, if we insert an int array 123 into the List collection, the IDE will report an error and fail to compile. The key difference is that in the statement List When assembling, we also need to declare for it List The object type of the data in the collection. The List class is the generic equivalent of the ArrayList class, and most of its usage is similar to the ArrayList because the List class also inherits the IList interface. Such as: Array animalArray = new Array ĪrrayList animalArrayList = new ArrayList() īecause ArrayList has the shortcomings of unsafe types and boxing and unboxing, the concept of generics appears. You need to specify the length when you define it the usage of ArrayList is the same as a normal collection, you don’t need to specify the length when you define it The usage of Array is almost the same as that of array, which can be regarded as an array. The process of packing and unpacking is very lossy.
![c sharp array vs arraylist c sharp array vs arraylist](https://www.dotnetcurry.com/images/csharp/hashset/hashset-duplicate-eliminate.png)
![c sharp array vs arraylist c sharp array vs arraylist](https://askanydifference.com/wp-content/uploads/2021/01/ArrayList-vs-Array-in-C.jpg)
Specify: string i=(string)obj //About unboxing was originally written like this, now it is corrected to int j = (int) obj Int j = ( int ) obj ; // Unboxing: Convert reference type obj to value type j Unboxing: is to extract the value type from the reference data: for example, assign the value of the object object obj to the variable j of type int object obj=”abc” The concept of boxing and unboxing: Simply put: Boxing: Packing the data of the value type into the instance of the reference type, for example, assigning the value abc of the string type to the object object obj Boxing and unboxing operations usually occur when storing or retrieving value types, which brings a lot of energy loss.
![c sharp array vs arraylist c sharp array vs arraylist](https://csharp-station.com/wp-content/uploads/2016/08/C-Arrays-vs-Lists.jpg)
Because ArrayList treats all data inserted into it as an object type, when we use ArrayList to process data, it is likely to report a type mismatch error, that is, ArrayList is not type safe. In this way, it is allowed to insert different types of data in the ArrayList. Therefore, you do not need to specify its length when declaring an ArrayList object.įrom the above example, ArrayList seems to solve all the shortcomings in the array, why is there a List again?įrom the above example, we can see that in the List, we not only inserted the string cde, but also the number 5678.
![c sharp array vs arraylist c sharp array vs arraylist](https://www.jquery-az.com/wp-content/uploads/2018/09/8.4-c_Arraylist-Insert.png)
The size of the ArrayList object is dynamically expanded and contracted according to the data stored in it. ArrayListĪrrayList is a namespace System.CollectionsIn the next part, it must be quoted when using this class, and it inherits the IList interface to provide data storage and retrieval. In view of these shortcomings of arrays, C# first provided the ArrayList object to overcome these shortcomings. If we don't know the length of the array when declaring the array, it will become very troublesome. If the length of the array is too long, it will cause a waste of memory, and over-segment will cause data overflow errors. It is very troublesome to insert data between the two data of the array, and the length of the array must be specified when declaring the array.