During the interview , Suddenly asked List<> and ArrayList The difference between , Although the point is made , But I know the answer is more than that . So it's important to check the data , Organized the array ,List and ArrayList The difference between .

array

   The first thing to say about arrays is to sort out the collection classes :Array.Array Class is C# Base class for all arrays in , It's in System Defined in namespace .Array
Class provides various properties and methods for arrays .
   Properties such as : IsReadOnly Array read only .Length Array length .
   The common methods are as follows :
1 Clear
According to the type of element , Set the element of a range in the array to zero , by false Or null.
2 Copy(Array, Array, Int32)
Copy a range of elements from the first element of an array to the first element of another array . Length by one 32 Bit integer assignment .
3 CopyTo(Array, Int32)
Copies all elements from the current one-dimensional array to the specified index position of a specified one-dimensional array . The index is composed of a 32 Bit integer assignment .
4 GetLength
Get one 32 Bit integer , This value represents the total number of elements in the array of the specified dimension .
7 GetType
Get the type of the current instance . From object (Object) inherit .
9 GetValue(Int32)
Gets the value of a specified position in a one-dimensional array . The index is composed of a 32 Bit integer assignment .
10 IndexOf(Array, Object)
Search for the specified object , Returns the first index in the entire one-dimensional array .
11 Reverse(Array)
Reverse the order of elements in an entire one-dimensional array .
12 SetValue(Object, Int32)
Sets a value for an element at a specified position in a one-dimensional array . The index is composed of a 32 Bit integer assignment .
13 Sort(Array)
Using the IComparable Implementation to sort elements in the entire one-dimensional array .
14 ToString
Returns a string representing the current object . From object (Object) inherit .

How to use it :
int[] nums = {1,2,3,4,5 }; Array.Sort(nums);
   Continuous storage in memory , So its index speed is very fast , And it's easy to assign and modify elements , such as :nums[2]=5.

   But there are some disadvantages in arrays . For example, it's very troublesome to insert data between two data of an array , And when we declare arrays , The length of the array must also be specified , Array is too long , Will cause memory waste , Array and length too short , Data overflow error . So when we declare an array, we don't know the length of the array , It's a lot of trouble .C# First of all ArrayList Object to overcome these shortcomings .

ArrayList

  ArrayList Dynamic array , yes .Net
Framework Special classes provided for data storage and retrieval , It's a namespace System.Collections Next part . Its size is dynamically expanded and contracted according to the data stored in it . therefore , We're saying ArrayList Object without specifying its length .ArrayList Inherited IList Interface , So it is very convenient to add data , Insert and remove . such as :
  
ArrayList al = new ArrayList(); // New data al.Add("abc"); al.Add(123); // Modify data al[2
] =345; // Remove data al.RemoveAt(0); // insert data al.Insert(0, "hello world");
From the above example ,ArrayList It seems to solve all the shortcomings in the array , So it should be perfect , Why C#2.0 It'll come back later List What about ?

List

   stay ArrayList in , We're not just inserting strings ”abc”, And the numbers are inserted 123. 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 . such , In our use ArrayList When the data in , It is likely to report type mismatch errors , in other words ArrayList Not type safe . Even if we promise to be careful when inserting data , All have the same type of data inserted , But in use , We also need to transform them into corresponding original types to handle . There is the operation of packing and unpacking , Great performance loss .

   Because ArrayList Disadvantages of unsafe type and unpacking , So in C#2.0 Then came the concept of generics . and List Class is ArrayList Generic equivalent class of class . Most of its uses are related to ArrayList be similar , because List Class also inherits IList Interface . The key difference is , In the statement List When assembling , We also need to declare for it List Object type of data in the collection .
such as :
  
List<int> list = new List<int>(); // New data list.Add(123); // Modify data list[0] = 345;
// Remove data list.RemoveAt(0);
   In the above example , If we go List Insert in generic collection string character ”hello
world”,IDE It's a mistake , And cannot be compiled . In this way, the type safety problems mentioned above and the performance problems of packing and unpacking are avoided .

 List Benefits of generics :

   By allowing specific types of generic class or method operations to be specified , Generics transfer the task of type safety from you to the compiler . You don't need to write code to check that the data type is correct , Because the correct data type is enforced at compile time . Reduces the need for type casts and the possibility of runtime errors . Generics provide type security without increasing the overhead of multiple implementations .

summary :
All three are used to process a set of data , Inherited IEnumerable Interface , Available for for Cyclic treatment . You can get and set elements through index Subscripts .

Arrays are continuous in memory , Fixed length , Cannot delete and add new elements . and ArrayList or List Capacity to automatically insert and delete elements 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 .

Specific types (Object except ) Array of Better performance 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 similar to that of arrays of the same type .

In the decision to use List Still use ArrayList class ( Both have similar functions ) Time , remember List 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 .

Technology
©2019-2020 Toolsou All rights reserved,
html Writing about cherry trees , Writing about cherry trees It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Browser kernel ( understand )HashMap Explain in detail java Four functional interfaces ( a key , simple )os Simple use of module Some East 14 Pay change 16 salary , Sincerity or routine ?