In C #, when we want to store a set of objects, we think of using arrays to arraylist,list the three objects. So what's the difference between these three people?
Let's look at the array first, because the array is the first occurrence in C #.
Array
Arrays have many advantages, such as arrays are stored continuously in memory, so the index speed is very fast, and the assignment and modification elements are simple, such as:
string[] s=New string[3];//Assign Values[0]="a"; s[1]="b"; s[2]="C";//Modifys[1]="B1";
However, there are some deficiencies in the array. For example, inserting data between two data in an array is cumbersome. And when we declare an array, we must also indicate the length of the array, the length of the array, the memory wasted, the array and the length too short, causing the data overflow error. So if we don't know the length of the array when we declare the array, it becomes tricky.
For these drawbacks of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.
ArrayList
ArrayList is a dedicated class provided by the. Net Framework for data storage and retrieval, which is part 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 declaring it.
ArrayList inherits the IList interface, so it can be easily added, inserted, and removed from the data. For example:
ArrayList list =NewArrayList ();//New DataList. ADD ("ABC"); list. ADD (123);//Modifying Datalist[2] =345;//Removing DataList. RemoveAt (0);//Inserting DataList. Insert (0,"Hello World");
From the above example, ArrayList seems to solve all the shortcomings in the array, then it should be perfect, why the list after the c#2.0?
Or from the example above, in list, we not only inserted the string "abc", but also inserted the number 123. This allows the insertion of different types of data in the ArrayList. Because ArrayList will handle all the data inserted into it as an object type. Thus, when we use the data in ArrayList to handle the problem, it is possible to report a type mismatch error, that is, ArrayList is not type-safe . Even though we are guaranteed to be careful when inserting data, we have inserted the same type of data, but in use, we also need to convert them to the corresponding primitive type to handle. This is the case of boxing and unpacking operation, will bring a lot of performance loss.
Interspersed with the concept of boxing and unpacking:
In simple terms:
Boxing: is to package data of a value type into an instance of a reference type
For example, the value 123 of the int type is assigned to the object o
int i=123; Object o= (object) I;
Unpacking: Extracting value types from reference data
For example, assigning the value of the object o to the variable I of type int
Object o=123; int i= (int) o;
the process of packing and unpacking is very lossy .
Generic List
It is because ArrayList has the disadvantage of unsafe type and packing unpacking, so the concept of generics appears after c#2.0. The list class is a generic equivalent class of the ArrayList class. Most of its usage is similar to ArrayList, because the list class inherits the IList interface as well. The key difference is that when declaring a list collection, we also need to declare the object type for the data in the list collection.
Like what:
list<intnew list<int>(); // New Data list. ADD (123); // modified data list[0345; // removes the data list. RemoveAt (0);
In the example above, if we insert the string character "Hello World" into the list collection, the IDE will report an error and cannot be compiled. This avoids the previously mentioned type-safety issues and the performance issues of packing unboxing.
Summarize:
The capacity of an array is fixed, you can only get or set the value of one element at a time, and the capacity of ArrayList or list<t> can automatically expand, modify, delete, or insert data as needed.
An array can have multiple dimensions, and ArrayList or list< t> always have only one dimension. However, you can easily create lists of array lists or lists. The performance of arrays of specific types (except Object) is better than ArrayList. This is because the elements of ArrayList are of type Object, so boxing and unboxing operations typically occur when a value type is stored or retrieved. However, when redistribution is not required (that is, the initial capacity is very close to the maximum capacity of the list),list< t> has the same performance as an array of the same type.
When deciding whether to use List<t> or the ArrayList class, which has similar functionality, remember that the List<t> class performs better and is type-safe in most cases. If you use a reference type for type T of the list< t> class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.
The difference between arrays, ArrayList, and list in C #