Array in C #, what is the difference between ArrayList and List object?

Source: Internet
Author: User

In C #, when we want to store a group of objects, we will think of using arrays, ArrayList, and List. So what are the differences between the three?

Let's take a look at the array first, because the array first appeared in C.

Array

Arrays have many advantages. For example, arrays are stored continuously in the memory, so their indexing speed is very fast, and the assignment and modification elements are also very simple. For example:

[Csharp]
String [] s = new string [3];
// Assign a value
S [0] = "";
S [1] = "B ";
S [2] = "c ";
// Modify
S [1] = "b1 ";

String [] s = new string [3];
// Assign a value
S [0] = "";
S [1] = "B ";
S [2] = "c ";
// Modify
S [1] = "b1 ";
 

However, arrays also have some shortcomings. For example, it is difficult to insert data between two data arrays. In addition, when declaring an array, we must specify the length of the array at the same time. The length of the array is too long, leading to a waste of memory. The array and length are too short, leading to data overflow errors. In this way, if we do not know the length of the array when declaring the array, it will become very tricky.

To address these disadvantages of arrays, The ArrayList object is first provided in C # To overcome these disadvantages.


ArrayList

ArrayList is a specialized class provided by. Net Framework for data storage and retrieval. It is part of the namespace System. Collections. Its size is dynamically expanded and reduced according to the data stored in it. Therefore, we do not need to specify the length of an ArrayList object when declaring it.

ArrayList inherits the IList interface, so it can easily add, insert, and remove data. For example:

[Csharp]
ArrayList list = new ArrayList ();
// Add data
List. Add ("abc ");
List. Add (1, 123 );
// Modify data
List [2] = 345;
// Remove data
List. RemoveAt (0 );
// Insert data
List. Insert (0, "hello world ");

ArrayList list = new ArrayList ();
// Add data
List. Add ("abc ");
List. Add (1, 123 );
// Modify data
List [2] = 345;
// Remove data
List. RemoveAt (0 );
// Insert data
List. Insert (0, "hello world ");
As shown in the above example, the ArrayList seems to solve all the shortcomings in the array, so it should be perfect. Why does the List appear after C #2.0?

As shown in the above example, in the list, we not only inserted the string "abc", but also inserted the number 123. In this way, it is allowed to insert different types of data in the ArrayList. Because ArrayList treats all data inserted into it as the object type. In this way, when we use the data in the ArrayList to handle the problem, it is likely that the error of Type Mismatch will be reported, that is, the ArrayList is not of type security. Even though we make sure that we are very careful when inserting data, we have inserted the same type of data, but in use, we also need to convert them to the corresponding original type for processing. This results in the packing and unpacking operations, resulting in high performance loss.

Interspersed with the concepts of packing and unpacking:
To put it simply:
Packing: package data of the value type into the instance of the reference type.
For example, assign 123 of the int type value to the object o

Int I = 123;
Object o = (object) I;

Binning: Extracts value types from referenced data.
For example, assign the o value of the object to the int type variable I

Object o = 123;
Int I = (int) o;

The process of packing and unpacking is very performance-consuming.


Generic List

It is precisely because ArrayList has the disadvantages of unsafe types and packing and unpacking, so the concept of generics emerged after C #2.0. The List class is the generic 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]
List <int> list = new List <int> ();
// Add data
List. Add (1, 123 );
// Modify data
List [0] = 345;
// Remove data
List. RemoveAt (0 );

List <int> list = new List <int> ();
// Add data
List. Add (1, 123 );
// Modify data
List [0] = 345;
// Remove data
List. RemoveAt (0 );
In the above example, if we insert the string character "hello world" into the List set, the IDE will report an error and cannot pass the compilation. This avoids the aforementioned type security issues and the performance problems of packing and unpacking.

 

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.