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.