stay c# in , Arrays are continuous in memory , Indexing is fast , Assignment and modification are also very simple .string s=new string; // assignment s="a";
s="b"; // modify s="a1";
But there are some disadvantages in arrays , Inserting data between two pieces of data is cumbersome , And the length of the array must be specified when declaring the array , Too long array length will cause memory waste , Too short will cause memory overflow . If we don't know the length of an array when we declare it , It's going to be a lot of trouble ,
For these disadvantages of array ,c# First of all ArrayList Object to overcome these shortcomings .
ArrayList Is namespace System.Collections Next part , Must be referenced when using this class , At the same time inherited IList Interface , Provides data storage and retrieval .ArrayList The size of an object is dynamically expanded and shrunk according to the data stored in it . therefore , In the statement ArrayList Object without specifying its length .
ArrayList list1 = new ArrayList(); // New data list1.Add("cde"); list1.Add(5678);
// Modify data list = 34; // Remove data list.RemoveAt(0); // insert data list.Insert(0, "qwe");
From the above example ,ArrayList Solves all array shortcomings , So why did it happen list What about ?
In the above example ,ArrayList Not only are strings inserted code, And the numbers are inserted 5678, It's like this ArrayList Inserting different types of data into is allowed . because ArrayList All data inserted into it will be treated as object Type to handle , In our use ArrayList When processing data , Type mismatch is likely , that is ArrayList Not type safe . When storing or retrieving types , The operation of packing and unpacking usually leads to great performance loss .
Concept of packing and unpacking ：
In a nutshell ：
Packing ： It is to package the data of value type into the instance of reference type
For example string Value of type abc Assign to object object obj
The process of packing and unpacking is very destructive .
generic paradigm List
because ArrayList Disadvantages of unsafe type and unpacking , therefore
The concept of generics has emerged .List Isochronous ArrayList Generic equivalent class of class . Most of its users ArrayList be similar , because List Class also inherits IList Interface . The key difference is , In the statement List When assembling , Our colleagues need to make a statement List Object type of data in the collection ：
such as ：
// New data list.Add(“abc”); // Modify data list = “def”; // Remove data list.RemoveAt(0);
In the above example , If we go List Insert in collection int array 123,IDE It's a mistake , And it can't be compiled . In this way, the type safety problems mentioned above and the performance problems of packing and unpacking operation are avoided .
The capacity of an array is fixed , You can only get or set the value of one element at a time , and ArrayList or List<T> The capacity of can be automatically expanded as needed , modify , Delete or insert data .
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 . Specific types (object except ) The performance of array of is better than ArrayList Performance of . that is because ArrayList Element of Object type ; Therefore, 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 ）,List<T>
The performance of is very close to that of arrays of the same type .
In the decision to use List<T>
Still use ArrayList class （ Both have similar functions ） Time , remember List<T> Classes perform better in most cases and are type safe . If yes List<T> Classes perform better and are type safe in most cases . If yes List<T> Type of class T Use reference type , The behavior of the two classes is exactly the same . however , If for type T Use value type , Then we need to consider the implementation and packing problems .