Dark Horse Programmer -- Yan Zhendong ---------------------- ASP. NET + unity3d game development,. Net training, hope to communicate with you! -------------------- First, both hashtable and dictionary belong to the dictionary class, which is the set of key-value pairs. Similarities: we can add key-value pairs to them using the add () function. And addrange () functions. You can also remove move moverange moveat and use the key to find the value. Differences: For value types, the performance of a dictionary of a specific type (excluding objects) is better than that of hashtable, because the hashtable element belongs to the object type, therefore, the packing and unboxing operations are often triggered when the value type is stored or retrieved.
The dictionary class and the hashtable class have the same functions.
Hashtable ht = new hashtable (); // implement the idictionary Interface
Ht. Add (1, "");
Ht. Add (2, "B ");
Ht. Add (3, "C ");
Foreach (dictionaryentry de in HT) // hashtable returns the dictionaryentry type
{
De. Key;
De. value;
}
Dictionary <int, string> mydictionary = new dictionary <int, string> (); // implements the idictionary interface and the idictionary <t key, T value> class.
Mydictionary. Add (1, "");
Mydictionary. Add (2, "B ");
Mydictionary. Add (3, "C ");
Foreach (int I in mydictionary. Keys)
{
Console. writeline ("Key =" + I + "value =" + mydictionary );
}
Or
Foreach (keyvaluepair <string, double> temp in mydictionary) // The returned keyvaluepair <string, double> Generic Array
{
Temp. Key;
Temp. value;
}
Both get values through keys through loops.