The difference between array, ArrayList, List<T> in C#

table of Contents

Array

ArrayList

List (generic List)

to sum up


In C# array, ArrayList, List<T> can store a set of objects, so what is the difference between these three?

Array

Array first appeared in C#. It is stored continuously in memory, so its indexing speed is very fast, and the assignment and modification of elements are also very simple.

<span style="font-family:SimSun;font-size:18px;">//数组  
string[] s=new string[2];  
  
//赋值  
s[0]="a";  
s[1]="b";  
//修改  
s[1]="a1";  
</span>  

 But the array has some shortcomings. Inserting data between the two data of the array is very troublesome, and the length of the array must be specified when declaring the array. If the length of the array is too long, it will cause a waste of memory, and over-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.

In view of these shortcomings of arrays, C# first provided the ArrayList object to overcome these shortcomings. 

ArrayList

 ArrayList is a part of the namespace System.Collections. It 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 in it. Therefore, you do not need to specify its length when declaring an ArrayList object.

<span style="font-family:SimSun;font-size:18px;">//ArrayList  
ArrayList list1 = new ArrayList();  
  
//新增数据  
list1.Add("cde");  
list1.Add(5678);  
  
//修改数据  
list[2] = 34;  
  
//移除数据  
list.RemoveAt(0);  
  
//插入数据  
list.Insert(0, "qwe");  
</span>

From the above example, ArrayList seems to solve all the shortcomings in the array. Why is there a List again?

We can see from the above example, in the List, we not only inserted the string cde, but also inserted the number 5678. In this way, it is allowed to insert different types of data in the ArrayList . Because ArrayList treats 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, which brings a lot of energy consumption .

Simply talk about the concept of "boxing and unboxing":
        boxing: is to pack the value type data into the instance of the reference type,
       such as assigning the string type value abc to the object object obj

<span style="font-family:SimSun;font-size:18px;">String  i=”abc”;  
object obj=(object)i;  
</span>  

    Unboxing: is to extract the value type from the reference data,
    such as assigning the value of the object object obj to the variable i of type string

<span style="font-family:SimSun;font-size:18px;">object obj=”abc”;  
string i=(string)obj;  
</span> 

The process of packing and unpacking is very lossy

List<T> (generic List)

Because ArrayList has the disadvantages of unsafe types and boxing and unboxing, the concept of generics appears. 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. The key difference is that when declaring the List collection, we also need to declare the object type of the data in the List collection for it.

such as:

<span style="font-family:SimSun;font-size:18px;">List<string> list = new List<string>();  
  
//新增数据  
list.Add(“abc”);  
  
//修改数据  
list[0] = “def”;  
  
//移除数据  
list.RemoveAt(0);  
</span>  

In the above example, if we insert the int array 123 into the List collection, the IDE will report an error and fail to compile. In this way, the type safety problems and the performance problems of packing and unpacking mentioned above are avoided.

to sum up

      1.   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 ArrayList or List<T> can be automatically expanded, modified, deleted or inserted as needed .

      2. An  array can have multiple dimensions, and an ArrayList or List<T> always has only one dimension . However, you can easily create an array list or a list of lists . The performance of arrays of specific types (except Object) is better than that of ArrayList . This is because the elements of ArrayList belong to the Object type; so boxing and unboxing operations usually occur when storing or retrieving value types. However, when reallocation is not required (that is, the initial capacity is very close to the maximum capacity of the list), the performance of List<T> is very similar to that of arrays of the same type.

      3. When deciding whether to use the List<T> or the ArrayList class (both have similar functions), remember that the List<T> class performs better than the ArrayList class in most cases and is type-safe . If you use a reference type for the type T of the List<T> class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.

 

Guess you like

Origin blog.csdn.net/Dust_Evc/article/details/114984023