A brief discussion of code examples of Hashtable and Dictionary in C#:
Dictionary
Hashtable is not generic and not type-safe; Dictionary is generic and type-safe;
The key values of Hashtable are all of Object type, but the data type of Dictionary key values can be specified.
In other words, if a data type other than Object is stored in the Hashtable, when the data is taken out, it needs to be displayed in type conversion before it can be used normally. Dictionary does not have this problem.
In this respect, Hashtable is equivalent to Dictionary
Hashtable ht = new Hashtable(); Dictionary<string, int> dic = new Dictionary<string, int>(); ht.Add("A", 1); dic.Add("A", 1); //Console.WriteLine(ht["A"]+1); //编译错误!Object类型不能和int类型直接进行相加。 Console.WriteLine((int)ht["A"] + 1);//编译通过,输出结果为:2 Console.WriteLine(dic["A"] + 1); //编译通过,输出结果为:2
The consistency of the order of reading data in Dictionary and Hashtable and the order of data when adding data cannot be guaranteed, or it can be said that there is no consistency.
Dictionary can keep the order of reading data consistent with the order when adding when only adding but not deleting;
But after deletion and addition operations, it is no longer guaranteed that the order of reading data is the same as the order when adding.
Dictionary<int, int> dic = new Dictionary<int, int>(); dic.Add(0, 0); dic.Add(1, 1); dic.Add(2, 2); Console.WriteLine("仅仅经过添加元素处理之后:"); foreach (KeyValuePair<int, int> kvp in dic) { Console.WriteLine("Key:" + kvp.Key + " Value:" + kvp.Value); } dic.Remove(0); dic.Add(3, 3); Console.WriteLine("经过删除和添加元素处理之后:"); foreach (KeyValuePair<int, int> kvp in dic) { Console.WriteLine("Key:" + kvp.Key + " Value:" + kvp.Value); } Console.ReadKey();
For Dictionary, if an element is deleted from it, the newly added element will fill the position of the deleted element, thus causing the order of adding data to be inconsistent with the order of reading data.
For Hashtable, its data storage order is calculated according to a certain algorithm, so in most cases, its data reading order and data adding order are inconsistent.
So if you need to maintain the order when data is added, it is best not to use Dictionary and Hashtable.
For Hashtable, if a non-existent Key value is used to obtain the value, a null will be returned;
Hashtable ht = new Hashtable(); Console.WriteLine(ht["b"]==null); Console.ReadKey();
For Dictionary, if a non-existent Key value is used to obtain the value, an exception of type "System.Collections.Generic.KeyNotFoundException" will be thrown.
Therefore, when getting a value from a Dictionary or Hashtable, you can first determine whether the Key value exists (use the ContainsKey() method to determine) to prevent unexpected values or exceptions.
Dictionary is not thread-safe, Hashtable is thread-safe.
The above is the detailed content of A brief discussion of code examples of Hashtable and Dictionary in C#. For more information, please follow other related articles on the PHP Chinese website!