Arraylist of dictionary C#

Foreword

At work, I often encounter C # arrays, arraylist, limited, Dictionary access data, but which type of choice is stored data, I have never known how to go to beginners. So I took a good look and compared to their usage and comparison, summed it here, and there is a need to improve again.

initialization

Array:

ArrayList:

ArrayList buff = new ArrayList();

List:

List buff = new List();

Dictionary:

Dictionary buff = new Dictionary;

analyse and compare

  It can be seen from several types that initialize above, they are all reference types. Where arrays, List, Dictionary needs to specify its element type when initialization, and ArrayList does not need to specify the type. Only the array there is a size when it is initialized.

Array: You must specify the size and type when initialization, and he is continuously stored in memory, so you can see that the index speed of the array is very fast. After determining the length and type of the array, selecting array storage data is a better choice. Not suitable for insertion operation.

ArrayList: Do not specify its size and type when initialization. He can store different data types, but in the process of deployment, it will cause packing and unpacking, reducing performance. Convenient insertion operation.

List: You must specify its type when initialization, but do not need to specify the size, so he will not cause packing and unpacking operations in the access process like ArraryList. List and array performance is quite in the same case of type. Convenient insertion operation.

Dictionary: You must also specify its type while initialization, and he also needs to specify a key, and this key is unique. Because of this, Dictionary's index speed is very fast. But because he added a key, the memory space occupied by Dictionary is larger than other types. He is looking for elements through key, and the order of the elements is uncertain.

Types ofDetermineDetermined typeIndex speedperformance
ArrayYYquicklyhighest
ArrayListNNgenerallow
ListNYquicklyhigh
DictionaryNYFastestgeneral

Note: If the above content is not appropriate, please leave a message, share knowledge, and grow up!