Simple introduction to the usage and differences of arrays, ArrayList, lists, dictionary in C #

Source: Internet
Author: User
The C # array, ArrayList, List, dictionary Access data are often encountered in the work, but which type of data is chosen for storage? Very confused, today's small series of time to everyone to clean up this aspect of the content, the need for friends to refer to it

Objective

In the work often encountered C # array, ArrayList, List, dictionary access data, but the choice of which type to store data, for beginners I always do not know how to choose. So the time to take a good look at their usage and comparison, summarized here, there is a need to improve the re-update.

Initialization

Array:

int[] buff = new INT[6];

ArrayList:

ArrayList buff = new ArrayList ();

List:

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

Dictionary:

dictionary<int,string> buff = new dictionary<int,string>;

Analysis and comparison

As you can see from the types initialized above, they all belong to reference types. Where arrays, lists, and dictionary need to specify their element types when initialized, and ArrayList do not need to specify a type. In which only the array is set to its size at initialization time.

Array: You must specify its size and type at initialization, which is stored continuously in memory, so you can see that the index speed of the array is very fast. Selecting an array to store data is a good choice after determining the length and type of the array. Not suitable for insert operations.

ArrayList: You do not need to specify its size and type at initialization time. He can store different data types, but it will cause boxing and unboxing during the deposit process, reducing performance. Easy insert operation.

List: The type must be specified at initialization time, but it does not need to be specified, so he will not cause boxing and unboxing in the access process, as in the case of arrarylist. In the same type of scenario, the list and array performance is equivalent. Easy insert operation.

Dictionary: The type must also be specified at initialization time, and he will need to specify a key, and the key is unique. Because of this, the dictionary index is very fast. But also because he added a key,dictionary occupies more memory space than other types. He finds elements by key, and the order of the elements is variable.

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.