I believe that arrays are the most commonly used in programming. No matter how many languages have data structures like arrays, C # is fully object-oriented, so the array in C # is also an object, which is actually an example of the array class. The use of the array class can be said to be the most frequently used, but you are not very concerned about it when using it, for example, an instance of an array object is actually created when an array int [] is created. Recently, I have carefully studied the similarities and differences between the array and arraylist classes in C #, and summarized the following points:
[Differences between array and arraylist]#1. variables of the array type must be instantiated at the same time (at least the size of the array must be initialized), while arraylist can only be declared first. For example: int [] array = new array [3]; or: int [] array = {1, 2, 3}; or: arraylist mylist = new arraylist (); these are legal, but using int [] array directly cannot. #2. Arrays can only store homogeneous objects, while arraylist can store heterogeneous objects. Homogeneous objects refer to the objects of the same type. If an array declared as int [], only integer data can be stored, and string [] can only be stored in plain data, except for arrays declared as object. The arraylist can store any different types of data (because it stores packed object objects, in fact, the arraylist uses "object [] _ items; "Such a private field to encapsulate the object) #3 storage method in the CLR managed pair. Array is always stored continuously, and the storage of arraylist is not necessarily consecutive. #4 initialization size. The size of the array object must be specified only during initialization, and the size of the created array is fixed. The size of the arraylist can be specified dynamically, and the size can be specified during initialization, you can also leave this parameter unspecified, that is, the space of the object can be increased at will. #5 arrays cannot add or delete items at will, while arraylist can insert or delete items anywhere.
[Similarity between array and arraylist]#1 all have indexes, that is, you can use indexes to directly obtain and modify any items. #2 all the objects they created are stored in the managed heap. #3 can enumerate itself (because both implement the ienumerable interface ).
[Features of arraylist]#1 I found an interesting phenomenon in studying arraylist. The capacity attribute value of arraylist changes with the actual size of the items in arraylist, as shown in the following code: public static void main (string [] ARGs) {arraylist mylist = new arraylist (2); console. writeline ("initial capacity:" + mylist. capacity); int size = 2; for (INT I = 0; I <size; I ++) {mylist. add (I);} console. writeline ("current capacity:" + mylist. capacity); console. readline ();} when the size is 2, "current capacity" in the output result is 2, and when the size is 3 or 4, "Current capacity" is 4, when the size is 5 ~ "Current capacity" is 8, and when the size is 9 ~ 16, "current capacity" is 16 ,... through experiments, we can draw a conclusion that every time there is an actual number of objects in the arraylist (arraylist. count) exceeds the capacity threshold, the threshold is automatically doubled. (You can also try to change the initial value when mylist is generated, but the conclusion is the same) #2. You can use the trimtoresize () method of the arraylist class to divide the null items in the arraylist instance by the compressed volume. Run the following code: public static void main (string [] ARGs) {arraylist mylist = new arraylist (5); For (INT I = 0; I <3; I ++) {mylist. add (I);} console. writeline ("actual capacity:" + mylist. capacity); mylist. trimtosize (); console. writeline ("compressed capacity:" + mylist. capacity); console. readline ();} output: actual capacity: 5 compressed capacity: 3 #3 in C #2.0, we recommend that you use the standard version of arraylist, that is, system. collection. list in the generics namespace <T> This not only ensures the type security, but also improves the object processing efficiency because there is no packing or unpacking process.