In C#, arrays, ArrayList, and List can store a group of objects, so what is the difference between these three?
Array
Arrays first appeared in C#. It is stored continuously in memory, so its indexing speed is very fast, and it is also easy to assign and modify elements.
//Arraystring[] s=new string[2]; //Assignments[0]="a"; s[1]="b"; //Revises[1]="a1";
But there are some shortcomings in the array. It is very troublesome to insert data between two data in an array, and when declaring an array, the length of the array must be specified. Too long the array will cause memory waste, and passing the segment will cause data overflow errors. If we don't know the length of the array when declaring the array, it will become very troublesome.
To address these shortcomings of arrays, the first ArrayList object is provided in C# to overcome these shortcomings.
ArrayList
ArrayList is part of the namespace, and must be referenced when using this class. It also inherits the IList interface and provides data storage and retrieval. The size of the ArrayList object is dynamically expanded and contracted according to the data stored therein. Therefore, it is not necessary to specify its length when declaring an ArrayList object.
//ArrayList ArrayList list1 = new ArrayList(); //Add new data("cde"); (5678); //Modify the datalist[2] = 34; //Remove data(0); //Insert data(0, "qwe");
Judging from the above example, ArrayList seems to solve all the shortcomings in the array, why does it have a List?
From the example above, in List, we insert not only the string cde, but also the number 5678. This allows inserting different types of data into the ArrayList. Because ArrayList will process all the 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. Boxing and unboxing operations usually occur when storing or retrieving value types, resulting in significant performance losses.
Concepts of packing and unboxing:
Simply put:
Boxing: It is to package the value type data into the instance of the reference type
For example, assign the value abc of type string to the object object obj
String i="abc"; object obj=(object)i;
Unboxing: It is to extract value types from referenced data
For example, assign the value of object object obj to a variable i of type string
object obj="abc"; string i=(string)obj;
The packing and unboxing process is very performance-depleting.
Generic List
Because ArrayList has the disadvantages of unsafe types and packing and unboxing, the concept of generics has emerged. The List class is a generic equivalent class of the ArrayList class. Most of its usage is similar to ArrayList because the List class also inherits the IList interface. The most critical difference is that when declaring a List collection, we also need to declare the object type of data in the List collection for it.
for example:
Listlist = new List (); //Add new data("abc"); //Modify the datalist[0] = "def"; //Remove data(0);
In the above example, if we insert int array 123 into the List collection, the IDE will report an error and cannot be compiled. This avoids the type safety issues mentioned above and the performance issues of packing and unboxing.
Summarize:
The capacity of the array is fixed, you can only get or set the value of one element at a time, and the capacity of the ArrayList or List<T> can automatically expand, modify, delete or insert data as needed.
An array can have multiple dimensions, while an ArrayList or List< T> always has only one dimension. However, you can easily create an array list or list of lists. Arrays of specific types (except Object) perform better than ArrayList. This is because the elements of an ArrayList belong to the Object type; therefore, boxing and unboxing operations usually occur when storing or retrieving value types. However, when there is no need to reassign (i.e., the initial capacity is very close to the maximum capacity of the list), List< T>'s performance is very similar to arrays of the same type.
When deciding whether to use List<T> or ArrayList classes (both have similar functionality), remember that the List<T> class performs better and is type-safe in most cases. If you use a reference type for type T of the List<T> class, the behavior of both classes is exactly the same. However, if you use a value type for type T, you need to consider implementation and boxing.
Thank you for reading, I hope it can help you. Thank you for your support for this site!