C # differences between arrays, ArrayList, and List

Source: Internet
Author: User

In C #, arrays, ArrayList, and List can all store a group of objects. What are the differences between the three. Array array first appears in C. It is stored continuously in the memory, so its indexing speed is very fast, and the assignment and modification of elements are also very simple. [Csharp] <span style = "font-family: SimSun; font-size: 18px;"> // array string [] s = new string [2]; // value s [0] = "a"; s [1] = "B"; // modify s [1] = "a1 "; </span> but the array has some limitations. It is very troublesome to insert data between two data arrays. When declaring an array, you must specify the length of the array. The length of the array is too long, which may cause a waste of memory, data overflow error may occur when the segment is too long. If we do not know the length of the array when declaring the array, it will become very troublesome. To overcome these disadvantages of arrays, The ArrayList object is provided in C. ArrayList is a part of the namespace System. Collections. It must be referenced when using this class and inherits the IList interface, which provides data storage and retrieval. The size of the ArrayList object is dynamically expanded and reduced according to the data stored in the object. Therefore, when declaring an ArrayList object, you do not need to specify its length. [Csharp] <span style = "font-family: SimSun; font-size: 18px;"> // ArrayList list1 = new ArrayList (); // Add list1.Add ("cde"); list1.Add (5678); // modify the data list [2] = 34; // remove the data list. removeAt (0); // insert data list. insert (0, "qwe"); </span> from the example above, the ArrayList seems to solve all the shortcomings in the array. Why is there a List? As shown in the preceding example, in the List, we not only insert the string cde, but also insert the number 5678. In this way, it is allowed to insert different types of data in the ArrayList. Because ArrayList processes all the data inserted into it as the object type, when we use ArrayList to process data, it is likely that the error of Type Mismatch will be reported, that is, ArrayList is not of type security. When storing or retrieving value types, the Box Packing and unboxing operations are often triggered, resulting in high performance consumption. The concept of packing and unpacking: To put it simply, packing: the data of the value type is packaged into the instance of the reference type. For example, the value abc of the string type is assigned to the object obj [csharp] <span style = "font-family: SimSun; font-size: 18px; "> String I =" abc "; object obj = (object) I; </span> binning: extract the value type from the referenced data. For example, assign the object obj value to the variable I [csharp] <span style = "font-family: SimSun; font-size: 18px; "> object obj =" abc "; string I = (string) obj; </span> the packing and unpacking processes are very performance-consuming. Because ArrayList has the disadvantages of unsafe type and packing and unpacking, the concept of generic type is introduced. The List class is a wildcard Equivalent Class of the ArrayList class. Most of its usage is similar to that of ArrayList, because the List class also inherits the IList interface. The most important difference is that when declaring a List set, we also need to declare the object type of data in the List set for it. For example: [csharp] <span style = "font-family: SimSun; font-size: 18px;"> List <string> list = new List <string> (); // Add a data list. add ("abc"); // modify the data list [0] = "def"; // remove the data list. removeAt (0); </span> In the above example, if we insert int array 123 into the List set, the IDE reports an error and cannot compile it. This avoids the aforementioned type security issues and the performance problems of packing and unpacking. Summary: The array capacity is fixed. You can only get or set the value of one element at a time, the capacity of ArrayList or List <T> can be automatically expanded, modified, deleted, or inserted as needed. Arrays can have multiple dimensions, while ArrayList or List <T> always has only one dimension. However, you can easily create an array or list. The performance of arrays of specific types (except objects) is superior to that of ArrayList. This is because the ArrayList element belongs to the Object type. Therefore, the binning and unboxing operations are usually generated when the value type is stored or retrieved. However, when no re-allocation is required (that is, the initial capacity is very close to the maximum capacity of the List), the performance of the List <T> is very similar to that of the array of the same type. When deciding whether to use the List <T> or the ArrayList class (both have similar functions), remember that the List <T> class performs better and is type-safe in most cases. If the type T of the List <T> class is referenced, the actions of the two classes are identical. However, if you use value type T, you need to consider implementation and packing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.