Hashtable class
I. Introduction
Indicates the set of key/value pairs. These key/value pairs are based on the hash of the key.Code.
Quick query is provided. The storage of elements is irrelevant to the sequence. An element cannot be inserted at the specified position because it does not have a valid sorting. It seems that its advantages are reflected in queries.
The hashtable key must be unique and has no valid sorting. It performs internal sorting.
Public class hashtable: idictionary, icollection, ienumerable,
Iserializable, ideserializationcallback, icloneable
Each element is a key/value pair stored in the dictionaryentry object. The key cannot be a null reference (nothing in Visual Basic), but the value can be.
Note: the so-called dictionaryentry structure defines dictionary key-value pairs that can be set or retrieved. It has a key attribute and a value attribute, representing the key and value respectively.
II. Add
Hashtable. Add Method
Public Virtual void add (
Object key,
Object Value
);
Add the elements with the specified key and value to hashtable.
Key of the element to be added.
The value of the element to be added. This value can be a null reference (nothing in Visual Basic ).
By setting the key value that does not exist in hashtable, the item attribute can also be used to add new elements. For example, mycollection ["mynonexistentkey"] = myvalue. However, if the specified key already exists in hashtable, setting the item attribute will overwrite the old value. In contrast, the add method does not modify existing elements.
Hashtable htable = new hashtable ();
Htable. Add ("1", "");
Htable. Add ("2", "B ");
Htable. Add ("3", "C ");
Htable. Add ("4", "D ");
Htable. Add ("5", "E ");
// Htable. Add ("1", "aaaaaaaaaaa"); // error because the key "1" already exists"
Htable ["1"] = "aaaaaaaaaaa"; // OK
Dropdownlist2.datasource = htable;
Dropdownlist2.datatextfield = "key ";
Dropdownlist2.datavaluefield = "value ";
Dropdownlist2.databind ();
II. Delete
1. Public Virtual void remove (
Object key
);
Removes elements with the specified key from hashtable.
If hashtable does not contain elements with the specified key, hashtable remains unchanged. No exception is thrown.
2. Public Virtual void clear ();
Remove all elements from hashtable.
Count is set to zero. The capacity remains unchanged.
3. Others
1. Public Virtual icollection values {Get ;}
Obtain the icollection that contains the values in hashtable.
Note: The icollection interface defines the size, enumeration number, and synchronization method of all sets.
Hashtable htable = new hashtable ();
Htable. Add ("1", "");
Htable. Add ("2", "B ");
Htable. Add ("3", "C ");
Htable. Add ("4", "D ");
Htable. Add ("5", "E ");
Label2.text = "";
Foreach (string strkey in htable. Keys) // traverse
{
Label2.text + = strkey;
Label2.text + = ",";
}
2. Public Virtual icollection keys {Get ;}
Obtain the icollection that contains keys in hashtable.
The returned icollection is not a static copy. On the contrary, icollection references the keys in the original hashtable.
Therefore, changes to hashtable are further reflected in icollection.
3. Public Virtual bool contains (
Object key
);
Determine whether hashtable contains a specific key.
4. Public Virtual bool containskey (
Object key
);
Determine whether hashtable contains a specific key. Same as contains
5. Public Virtual bool containsvalue (
Object Value
);
Determine if hashtable contains a specific value
6. Public Virtual int count {Get ;}
Obtain the number of key-value pairs contained in hashtable