In C # arrays, Arraylist,list can store a set of objects, so what is the difference between the three?
Array
The first occurrence of an array in C #. is stored continuously in memory, so it is indexed very quickly, and it is easy to assign and modify elements.
Array
string[] s=new string[2];
Assigning
A value s[0]= "a";
S[1]= "B";
Modify
s[1]= "A1";
But there are some deficiencies in the array. Inserting data between 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, the memory is wasted, and an overflow error can result. If we don't know the length of the array when we declare the array, it becomes cumbersome.
For these disadvantages of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.
ArrayList
ArrayList is a part of the namespace system.collections that must be referenced when it is used, while inheriting the IList interface, providing data storage and retrieval. The size of the ArrayList object is dynamically augmented and contracted according to the data stored in it. Therefore, you do not need to specify the length of the ArrayList object when declaring it.
ArrayList
ArrayList list1 = new ArrayList ();
New Data
List1. ADD ("CDE");
List1. ADD (5678);
Modify Data
list[2] =;
Removes the data
list. RemoveAt (0);
Inserts a data
list. Insert (0, "qwe");
From the example above, ArrayList seems to have solved all the flaws in the array, why is there a list?
From the example above, in the list, we not only inserted the string CDE, but also inserted the number 5678. This allows the insertion of different types of data in ArrayList. Because ArrayList will treat all the data inserted into the object type, when we use ArrayList to process the data, it is very likely that the 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 depletion.
The concept of boxing and unpacking:
To put it simply:
Boxing: is to package the data of a value type into an instance of a reference type
such as assigning the value ABC of type string to object obj
String i= "abc";
Object Obj= (object) I;
Unpacking: Extracting value types from reference data
such as assigning the value of object obj to the variable I of type string
Object obj= "ABC";
String i= (string) obj;
The process of packing and unpacking is very loss-performance.
Generic List
The concept of generics arises because the ArrayList has the disadvantage of unsafe types and box unboxing. The list class is a generic equivalent class of the ArrayList class, most of which are similar to ArrayList because the list class also inherits the IList interface. The key difference is that when declaring a list collection, we also need to declare the object type for which the data within the list collection is to be declared.
Like what:
List
list = new list
();
New Data
list. ADD ("abc");
Modify Data
list[0] = "def";
Removes the data
list. RemoveAt (0);
In the example above, if we insert an int array into the list collection, the error is 123,ide and cannot be compiled. This avoids the previously mentioned type safety problem and the performance problem of the boxed unboxing.
Summarize:
The capacity of an array is fixed, you can 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.
Arrays 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. Arrays of specific types (except for Object) perform better than ArrayList performance. This is because the elements of the ArrayList are of type Object, so boxing and unboxing operations usually occur when a value type is stored or retrieved. However, the performance of,list< t> is very similar to the same type of array when no reallocation is necessary (that is, the initial capacity is very close to the maximum capacity of the list).
When deciding whether to use List<t> or ArrayList classes (both have similar functionality), remember that the List<t> class performs better and is type-safe in most cases. If you use a reference type for the 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.
Thank you for reading, I hope to help you, thank you for your support for this site!