Home > Backend Development > C#.Net Tutorial > A brief introduction to the usage and differences of arrays, ArrayList, List, and Dictionary in C#

A brief introduction to the usage and differences of arrays, ArrayList, List, and Dictionary in C#

黄舟
Release: 2017-03-23 11:44:01
Original
1770 people have browsed it

I often encounter C# array, ArrayList, List, and Dictionary to access data, but which one should I choose? What type of data is stored? I'm very confused. Today, I took the time to sort out the content for you. Friends who need it can refer to it

Preface

At work I often encounter C# arrays, ArrayList, List, and Dictionary to access data, but as a beginner, I never know which type to choose to store data. So I took the time to take a closer look at their usage and comparison, and summarized it here. I will update it later if there are any improvements that need to be made.

Initialization

Array:

int[] buff = new int[6];
Copy after login

ArrayList:

ArrayList buff = new ArrayList();
Copy after login

List:

List<int> buff = new List<int>();
Copy after login

Dictionary:

Dictionary<int,string> buff = new Dictionary<int,string>;
Copy after login

Analysis and comparison

It can be seen from the several types initialized above that they all belong to the reference type. Among them, arrays, Lists, and Dictionaries need to specify their element types when initializing, while ArrayList does not need to specify the type. And in it only the array has its size set during initialization.

Array: Its size and type must be specified during initialization. It is stored continuously in memory, so it can be seen that the indexing speed of the array is very fast. After determining the length and type of the array, it is a better choice to choose an array to store data. Not suitable for insert operations.

 ArrayList: There is no need to specify its size and type during initialization. It can store different data types, but it will cause boxing and unboxing during the storage and retrieval process, which reduces performance. Easy to insert.

List: Its type must be specified during initialization, but the size does not need to be specified, so it will not cause boxing and unboxing operations during the access process like ArraryList. In the case of the same type, the performance of List and array is equivalent. Easy to insert.

 Dictionary: Its type must also be specified during initialization, and it also needs to specify a Key, and this Key is unique. Because of this, Dictionary indexing is very fast. But also because it adds a Key, Dictionary takes up more memory space than other types. It searches for elements through Key, and the order of the elements is uncertain.

The above is the detailed content of A brief introduction to the usage and differences of arrays, ArrayList, List, and Dictionary in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template