Analysis of array ArrayList and List objects in C

  • 2020-05-17 06:16:23
  • OfStack

Let's look at arrays 1 first, because arrays are the first to appear in C#.
An array of
Arrays have many advantages. For example, arrays are stored continuously in memory, so their indexing speed is very fast. Moreover, it is easy to assign and modify elements, such as:


string[] s=new string[3];
// The assignment 
s[0]="a";
s[1]="b";
s[2]="c";
// Modify the 
s[1]="b1";

However, arrays have some shortcomings. For example, inserting data between two pieces of data in an array can be cumbersome. In addition, when we declare the array, we must specify the length of the array at the same time. If the array is too long, memory will be wasted; if the array is too short, data overflow will be caused. This can be tricky if we don't know the length of the array when we declare it.
The ArrayList object is the first to be provided in C# to overcome these disadvantages of arrays.

ArrayList
ArrayList is a.Net Framework class for data storage and retrieval, which is part 1 of the namespace System.Collections. Its size is dynamically expanded and shrunk according to the data stored in it. Therefore, we do not need to specify the length of the ArrayList object when we declare it.
ArrayList inherits the IList interface, so it can easily add, insert and remove data. For example:


ArrayList list = new ArrayList();
// The new data 
list.Add("abc");
list.Add(123);
// Modify the data 
list[2] = 345;
// To remove data 
list.RemoveAt(0);
// Insert data 
list.Insert(0, "hello world");

From the above example, ArrayList seems to solve all the shortcomings of arrays, so it should be perfect. Why List after C#2.0?
Again, from the example above, in list, we not only inserted the string "abc", but also the number 123. This allows different types of data to be inserted into ArrayList. Because ArrayList treats all data inserted into it as if it were of type object. Thus, when we use the data in ArrayList to handle the problem, we are likely to report a type mismatch error, which means that ArrayList is not type safe. Even if we are careful to insert data of the same type, we need to convert them to the corresponding original type when using them. This results in the operation of packing and unpacking, which will bring about great performance loss.

Interspersed 1. Concepts of packing and unpacking:
To put it simply:
Boxing: packaging data of a value type into an instance of a reference type
For example, assign a value of type 123 of int to the object object o
int i=123;
object o=(object)i;
Unboxing: extracting value types from reference data
For example, assign the value of the object object o to a variable of type int, i
object o=123;
int i=(int)o;
The process of packing and unpacking is very inefficient.

Generic List
The concept of generics emerged after C#2.0 precisely because of the shortcomings of unsafe types and unboxing of ArrayList. The List class is the generic equivalent of the ArrayList class. Most of its usage is similar to ArrayList, because the List class also inherits the IList interface. The key difference is that when we declare the List collection, we also need to declare the object type for the data in the List collection.
Such as:


List<int> list = new List<int>();
// The new data 
list.Add(123);
// Modify the data 
list[0] = 345;
// To remove data 
list.RemoveAt(0);

In the example above, if we insert the string character "hello world" into the List collection, IDE will report an error and will not compile. This avoids the type safety issues discussed earlier and the performance issues of boxing and unboxing.


Related articles: