SoFunction
Updated on 2025-03-01

C# Detailed explanation of the differences between ArrayList, HashSet, HashTable, List, Dictionary

In C#, arrays are of fixed length, so they often cannot meet the needs of our development.

Because this limitation is inconvenient, an ArrayList appears.

ArrayList、List<T>

ArrayList is a variable-length array, you can add as much data as you want to add into the ArrayList. The internally maintained array will automatically expand to twice the original size when it is insufficient.

However, ArrayList also has a disadvantage, that is, the data stored in ArrayList are all of the Object type, so if the value type is stored and fetched, boxing and unboxing operations will occur (that is, the conversion between the value type and the reference type), which will affect the performance of the program. After the .Net 2.0 generics appear, List<T> is provided.

List<T> is a generic version of ArrayList. It no longer needs to be packed and unboxed. It is directly taken or used directly. It is basically the same as ArrayList. However, when using it, it must first set its type. After setting the type, data of this type is not this type, and Add is not allowed to enter.

In terms of performance, if there is only one kind of data to be stored in the array, then List<T> is undoubtedly the best choice.

List<int> ListInt = new List<int>();

If a variable-length array needs to be stored int and string. Then you can only use ArrayList.

HashTable (hash table), Dictionary<T,T>

HashTable is a key-value data structure that finds very fast based on keys. There cannot be duplicate keys. Moreover, due to its characteristics, its length is always a prime number, so the capacity after expansion will be a little larger than 2 times, and the loading factor is 0.72f.

When you need to use a large number of keys to find value, HashTable is undoubtedly the most selective. Like ArrayList, HashTable is non-generic. When the value is stored, it is an object. When the value is stored, it will be packed and unboxed, so Dictionary<T,T> appears.

Dictionary<T,T> is a generic version of HashTable, and access is equally fast, but no need to pack and unbox. Moreover, it optimizes the algorithm, Hashtable is 0.72, and its waste capacity is much less.

Dictionary<string,Person> Dic = new Dictionary<string,Person>();

HashSet<T>

The HashSet<T> class, algorithm, and storage structure are the same as the hash table. They are mainly designed for high-performance set operations, such as finding intersection, union, difference set for two sets. A collection contains a set of elements that do not appear repeatedly and have no specific order.

Queue、Queue<T>

Queue queues, Queue<T> generic queues, and have all been studied in college. Queue, first-in, first-out, which is very useful.

Stack、Stack<T>

Stack stack, first in and then out.

SortedList、SortedList<TKey,TValue>

The data in the SortedList collection is ordered. The data can be matched through keys, or the data can be obtained through int subscripts.

The addition operation is slightly slower than the ArrayList, and the Hashtable is slightly slower; the search and deletion operations are faster than the ArrayList, and slower than the Hashtable.

SortedDictionary<TKey,TValue>

SortedDictionary<TKey,TValue> Compared with SortedList<TKey,TValue>, its performance is optimized. SortedList<TKey,TValue> maintains an array internally while SortedDictionary<TKey,TValue> maintains a red and black tree (balanced binary tree), so its memory occupies better performance than SortedDictionary<TKey,TValue>. The only difference is that the value cannot be obtained by subscript.

ListDictionary (one-way linked list), LinkedList<T> (two-way linked list)

List<T>, ArrayList, Hashtable and other container classes maintain the array Array. ListDictionary and LinkedList<T> do not use Array, but save in the form of a linked list. The biggest advantage of linked lists is to save memory space.

ListDictionary is a one-way linked list.

LinkedList<T>Bidirectional linked list. The advantage of a two-way linked list can be inserted anywhere.

HybridDictionary

The HybridDictionary class makes full use of the advantages of Hashtable query efficiency and ListDictionary occupies less memory space. It has built-in two containers, Hashtable and ListDictionary. The internal logic when adding data is as follows:

When the data volume is less than 8, the Hashtable is null, and the data is saved with ListDictionary.

When the amount of data is greater than 8, instantiate the Hashtable, transfer the data to the Hashtable, and then set ListDictionary to null.

BitArray

BitArray is used for binary operations. Only true or false can be stored in operations such as "or", "non", "and", "exclusive or non".

Application scenarios

ArrayList,List<T>: variable-length array;

HashTable,Dictionary<T,T>: Frequently search value based on key;

HashSet<T>: set operation;

Queue, Queue<T>: First in, first out;

Stack, Stack<T>: Stack, first in, first out;

SortedList, SortedList<TKey,TValue>: When the hash table is to pass the subscript and to obtain the value through the key, it can be selected;

ListDictionary: One-way linked list. You must traverse the linked list every time you add data. When the data volume is large, the efficiency is low. When the data volume is large and the insertion is frequent, it is not suitable to choose.

LinkedList<T>: Bidirectional linked list;

HybridDictionary: Available when the data size is unknown.

SortedDictionary<TKey,TValue>: The optimized version of SortedList<TKey,TValue>, converting internal array into balanced binary tree.

BitArray: can be used in binary operations;

The above are all the knowledge points introduced this time. Thank you for your support.