An analysis of the differences between arrays, ArrayList and list objects in C #

Source: Internet
Author: User

Let's look at the array first, because the array is the oldest in C #.
Array
arrays have many advantages, such as that the array is stored continuously in memory, so its indexing speed is very fast, and the assignment and modification elements are also very simple, such as:

Copy Code code as follows:


string[] S=new string[3];


//Assign Value


s[0]= "a";


s[1]= "B";


s[2]= "C";


//Modify


s[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 amount of memory wasted, the array and the length too short, which can cause data overflow errors. So if we don't know the length of the array when we declare the array, it becomes tricky.
For these disadvantages of arrays, ArrayList objects are first provided in C # to overcome these drawbacks.

ArrayList
ArrayList is a specialized 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 contracted 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's easy to add, insert, and remove data.

Copy Code code as follows:


ArrayList list = new ArrayList ();


//New data


list. ADD ("abc");


list. ADD (123);


//Modify Data


LIST[2] = 345;


//Removal of data


list. RemoveAt (0);


//Insert Data


list. Insert (0, "Hello World");


From the example above, ArrayList seems to solve all the shortcomings in the array, then it should be perfect, why the list after c#2.0?
Or from the example above, in the list, we not only inserted the string "abc", but also inserted the number 123. This allows the insertion of different types of data in ArrayList. Because ArrayList will treat all the data inserted into it as Object type. In this way, when we use the data in ArrayList to deal with the problem, it is likely that the type mismatch error, which means that ArrayList is not type-safe. Even if we are careful when inserting data, we have inserted the same type of data, but we also need to convert them to the corresponding original type when we use them. This is the case of boxing and unboxing operation, will bring a lot of performance loss.

Interspersed with the concept of boxing and unpacking:
in simple terms:
Boxing: is to package the data of a value type into an instance of a reference type
For example, assign the value 123 of type int to object o
int i=123;
Object O= (object) I;
Unpacking: Extracting value types from reference data
such as assigning the value of object o to the variable I of type int
Object o=123;
int i= (int) o;
The process of packing and unpacking is very loss-performance.

Generic List
It is because the ArrayList has the disadvantage of unsafe type and boxed unboxing, so the concept of generics appears after c#2.0. The list class is the generic equivalent class of the ArrayList class. Most of its usage is 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:

Copy Code code as follows:


list<int> List = new list<int> ();


//New data


list. ADD (123);


//Modify Data


list[0] = 345;


//Removal of data


list. RemoveAt (0);


In the example above, if we insert the string character "Hello World" into the list collection, the IDE complains and cannot compile. This avoids the previously mentioned type safety problem and the performance problem of the boxed unboxing.

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.