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

The earliest appearance of arrays 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.

1 // array 2 string [] S = new string [2]; 3 4 // assign a value of 5 s [0] = ""; 6 s [1] = "B"; 7 // modify 8 s [1] = "A1 ";

However, 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

Arraylist is a part of the namespace system. Collections. It must be referenced when using this class. It also inherits the ilist interface and 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.

1 // arraylist 2 arraylist list1 = new arraylist (); 3 4 // Add data 5 list1.add ("CDE"); 6 list1.add (5678 ); 7 8 // modify data 9 list [2] = 34; 10 11 // remove data 12 list. removeat (0); 13 14 // insert data to 15 lists. insert (0, "qwe ");

From the example above, it seems that arraylist solves 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.

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 the value of the string type ABC to the object obj.

1 String  i=”abc”;2 object obj=(object)i;

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

1 object obj=”abc”;2 string i=(string)obj;

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

Generic list

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:

List <string> List = new list <string> (); // Add a data list. add ("ABC"); // modify the data list [0] = "def"; // remove the data list. removeat (0 );

In the above example, if we insert int array 123 into the list set, the IDE will report 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 arraylist or list <t> capacity 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.

Reprinted from: http://blog.csdn.net/zhang_xinxiu/article/details/8657431 #

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.