Recently, when using Hashtable and Dictionary in C #, I want to know the difference. I will make the following summary by searching relevant blog materials on the Internet.
Both Hashtable and Dictionary use different data structures as the carrier of key-value pairs. Like the ArrayList and LinkList in Java, although the List interface is implemented as a collection carrier, its internal structure is different. ArrayList is implemented through arrays, linkList is implemented through the object linked list.
Because Hashtable and Dictionary both exist at the same time, there must be selectivity in use scenarios, and they can be replaced at any time.
[1] Dictionary is recommended in a single-threaded program. It has the advantage of generics, fast reading speed, and sufficient capacity utilization.
[2] Hashtable is recommended in multi-threaded programs. The default Hashtable allows single-threaded writing and multi-threaded reading. For Hashtable, further calling the Synchronized () method can obtain a completely thread-safe type. dictionary is NOT thread-safe and must be manually protected using the lock statement, greatly reducing the efficiency.
[3] Dictionary has the ability to sort data by insertion Order (Note: however, when the Remove () API is called to delete a node, the order is disrupted ), therefore, it is convenient to use a Dictionary to reflect the order.
I prefer to use Dictionary because it uses generics and has high controllability. In some materials, many people also recommend the use of Dictionary, mainly for the following reasons:
1. Dic is type-safe, which helps us to write more robust and readable code, and saves us the trouble of forced conversion.
2. Dic is a wildcard row. When K or V is a value type, its speed far exceeds Hashtable. You do not need to perform explicit or implicit transformation when processing objects. You do not need to bind a value type when processing objects. Therefore, it is easier to use and more efficient (coding efficiency and running efficiency.
Before calling the Add method, Dictionary uses the ContainsKey method to test whether a key exists. Otherwise, a KeyNotFoundException is obtained. When the program frequently tries keys that do not exist in the dictionary, it uses the TryGetValue method to retrieve the value. This method is more effective.
The following are some common operations for Dictionary, including traversal, addition, and deletion.
[Csharp]
Dictionary <int, string> dictionary = new Dictionary <int, string> ();
Dictionary. Add (1, "xiaowang ");
Dictionary. Add (21, "dsd ");
Dictionary. Add (33, "dsfdfd ");
Dictionary. Add (4, "liusang ");
Foreach (KeyValuePair <int, string> kvp in dictionary)
{
Console. WriteLine ("key = {0}, value = {1}", kvp. Key, kvp. Value );
}
Foreach (int j in dictionary. Keys)
{
Console. WriteLine ("key = {0}, value = {1}", j, dictionary [j]);
}
If (dictionary. ContainsKey (2 ))
{
Console. WriteLine (dict [2]);
}
// Traverse Keys
Foreach (var item in dictionary. Keys)
{
Console. WriteLine ("Key: {0}", item );
}
// Traverse Values
Foreach (var item in dictionary. Values)
{
Console. WriteLine ("value: {0}", item );
}
<BR>