In C # arrays, Arraylist,list can store a set of objects, so what is the difference between the three?
Array
Arrays are first seen in C #. is stored continuously in memory, so its index is very fast, and the assignment and modification elements are simple.
[CSharp]View PlainCopy
- Array
- String[] s=new string[2];
- Assign value
- s[0]="a";
- s[1]="B";
- Modify
- s[1]="A1";
- </span>
But there are some deficiencies in the array. Inserting data between the two data in an array is cumbersome, and when declaring an array, you must specify the length of the array, the length of the array is too long, and the memory is wasted, which can cause data overflow errors. If we do not know the length of the array when declaring an array, it becomes cumbersome.
For these drawbacks of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.
ArrayList
ArrayList is part of the namespace System.Collections and must be referenced when using the class, inheriting the IList interface, providing data storage and retrieval. The size of the ArrayList object is dynamically expanded and shrunk according to the data stored in it. Therefore, you do not need to specify the length of the ArrayList object when declaring it.
[CSharp]View PlainCopy
- //arraylist
- arraylist list1 = new arraylist ();
-
- //new data
- list1. ADD (
- List1. ADD (5678);
- &NBSP;&NBSP;
- //modify data
- LIST[2]&NBSP;=&NBSP;34;&NBSP;&NBSP;
-
- //remove data
- list. RemoveAt (0);
- &NBSP;&NBSP;
- //insert data
- list. Insert (0,
- </span>
From the above example, ArrayList seems to solve all the shortcomings in the array, and why is there a list?
As we see from the above example, in list, we not only inserted the string CDE, but also inserted the number 5678. This allows the insertion of different types of data in the ArrayList. Because ArrayList will treat all of the data inserted as object types, when we use ArrayList to process data, it is possible to report a type mismatch error, that is, ArrayList is not type-safe. Boxing and unboxing operations typically occur when a value type is stored or retrieved, resulting in significant performance wear and tear.
Concept of packing and unpacking:
To put it simply:
Boxing: is to package data of a value type into an instance of a reference type
For example, assigning the value ABC of string type to the object obj
[CSharp]View PlainCopy
- String i= "ABC";
- Object Obj= (object) i;
- </span>
Unpacking: Extracting value types from reference data
For example, assigning the value of the object obj to the variable I of type string
[CSharp]View PlainCopy
- Object obj= "ABC";
- String i= (string) obj;
- </span>
The process of packing and unpacking is very lossy.
Generic List
The concept of generics occurs because ArrayList has the disadvantage of unsafe types and packing unboxing. The list class is a generic equivalent class of the ArrayList class, and 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:
[CSharp]View PlainCopy
- list<string> list = new list<string> ();
- New data
- List. ADD ("abc");
- modifying data
- List[0] = "Def";
- removing data
- List. RemoveAt (0);
- </span>
In the example above, if we insert an int array into the list collection 123,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 #