1. Definitions of NameValueCollection and HashTable
Hash list (Hash tableIs a data structure that is directly accessed based on the Key value. That is to say, It maps the key value to a location in the table to access records to speed up the search. This ing function is called a hash function, and the array storing records is called a hash function.
Advantages of HashTable table: HashTable is System. A container provided by the Collections namespace. The key/value in HashTable is of the object type, so HashTable can support any type of key/value pairs. The advantage is that the indexing method is fast.
NameValueCollectionThe class set is based on the NameObjectCollectionBase class. However, unlike NameObjectCollectionBase, this class stores multiple string values under one key (that is, if the key is the same, the values are connected as follows ). This class can be used for headers, query strings, and form data. Each element is a key/value pair. The capacity of NameValueCollection is the number of elements that can be saved by NameValueCollection.
The default initial capacity of NameValueCollection is zero. With the addition of elements to NameValueCollection, capacity increases automatically by reallocation as needed.
Example:
NameValueCollection myCol = new NameValueCollection ();
MyCol. Add ("red", "rojo"); // if the key value red is the same, merge the rojo, rouge
MyCol. Add ("green", "verde ");
MyCol. Add ("blue", "azul ");
MyCol. Add ("red", "rouge ");
2. Differences between NameValueCollection and Hashtable
1.
First, the former is not stored as a key-value pair. Therefore, this type of variable can store multiple names at the same time. When multiple names are stored, the value of this Name remains unchanged, but his Value will be appended to the previous
Values are separated by commas (,). Two identical key values are allowed in the same Hashtable, which follows the hash table in the data structure.
2. In terms of access, in NameValueCollection, you can use GetKey (I) to Get the Name of the specified index and Get (I) to Get the specified cable.
Hashtable stores each key-Value Pair in DictionaryEntry. Therefore, you need to use DictionaryEntry when performing value operations.
3. In the output order, the former is output in the storage order, while the latter is not output in the ascending order. The following describes how to perform simple operations on them:
A. Differences between references
Hashtable: using System. Collections;
NameValueCollection: using System. Collections. Specialized;
B. Duplicate keys
NameValueCollection: repeated.
HashTable is a key-value set, but the key cannot be repeated.
Hashtable ht = new Hashtable ();
Ht. Add ("key", "value ");
Ht. Add ("key", "value1"); // Error
Ht ["key"] = "value1"; // correct
C. Differences in Initialization
To initialize NameValueCollection, you must reference using System. Collections. Specialized;