Detailed explanation of the difference among array ArrayList and List in C

  • 2021-11-14 06:52:11
  • OfStack

In C #, arrays, ArrayList and List can all store one set of objects, so what is the difference between these three.

Array

Arrays first appeared in C #. It is stored contiguously in memory, so its indexing speed is very fast, and it is easy to assign values and modify elements.


// Array  
string[] s=new string[2]; 
// Assignment  
s[0]="a"; 
s[1]="b"; 
// Modify  
s[1]="a1"; 

However, there are some shortcomings in the array. It is very troublesome to insert data between two data in an array, 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 memory waste, and if it is too long, it will cause data overflow errors. If we don't know the length of the array when declaring it, it will become very troublesome.

To address these drawbacks of arrays, the ArrayList object was first provided in C # to overcome these drawbacks.

ArrayList

ArrayList is part 1 of the namespace System. Collections, which must be referenced when using this class, and inherits the IList interface to provide data storage and retrieval. The size of an ArrayList object is dynamically expanded and contracted according to the data stored in it. Therefore, when declaring an ArrayList object, it is not necessary to specify its length.


 //ArrayList 
ArrayList list1 = new ArrayList(); 
// Add data  
listAdd("cde"); 
listAdd(5678); 
 
// Modify data  
list[2] = 34; 
 
// Remove data  
listRemoveAt(0); 
 
// Insert data  
listInsert(0, "qwe"); 

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

From the above example, in List, we inserted not only the string cde, but also the number 5678. In this way, it is allowed to insert different types of data in ArrayList. Because ArrayList treats all data inserted into it as object type, when we use ArrayList to process data, we will probably 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 great performance loss.

Concepts of packing and unpacking;

To put it simply:

Boxing: Packing the data of a value type into an instance of a reference type

For example, the value abc of string type is assigned to the object object obj


String i= " abc " ; 
object obj=(object)i; 

Unpacking: Extracting value types from reference data

For example, assign the value of the obj object obj to the variable i of type string


object obj= " abc " ; 
string i=(string)obj; 

The process of packing and unpacking is very performance-consuming.

Generic List

Because ArrayList has the disadvantages of insecure types and boxed and unboxed, the concept of generics appears. The List class is a generic equivalent of the ArrayList class, and most of its usage is similar to that of the ArrayList class, because the List class also inherits the IList interface. The key difference is that when declaring an List collection, we also need to declare for it the object type of the data in the List collection.

For example:


List<string> list = new List<string>(); 
// Add data  
listAdd( " abc " ); 
// Modify data  
list[0] =  " def " ; 
// Remove data  
listRemoveAt(0); 

In the above example, if we insert int Array 123 into the List collection, 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.

Summary:

The size of the array is fixed, and you can only get or set the value of one element at a time, while ArrayList or List < T > The capacity of can automatically expand, modify, delete or insert data as needed.

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

After deciding to use List < T > When still using the ArrayList class (both have similar functions), remember List < T > Class performs better and is type safe in most cases. If List < T > Class type T uses a reference type, then the two classes behave exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.


Related articles: