Hashtable is a hash table used to represent a set of key values. These key-value pairs are organized based on the hash code of the key. Each element of the hashtable is stored in the key-value pair in the dictionaryentry object. The key cannot be a null reference.
Count: gets the number of key-value pairs contained in hashtable.
Isfixedsize: gets a value indicating whether the hashtable has a fixed size.
Isreadonly: gets a value indicating whether hashtable is read-only.
Issynchronized: gets a value indicating whether to synchronize access to hashtable.
Item: gets or sets the value associated with the specified key.
Keys: gets the icollection containing keys in hashtable.
Syncroot: Get the object that can be used to synchronize hashtable access
Values: Get the icollection containing hashtable
1. Add the hashtable Element
You can use the add method provided by hashtable.
Static void main (string [] ARGs)
{
Hashtable = new hashtable ();
Hashtable. Add ("ID", "bh001 ");
Hashtable. Add ("name", "TM ");
Hashtable. Add ("sex", "male ");
Console. writeline (hashtable. Count );
Console. Readline ();
}
2. Delete the hashtable Element
You can use the clear and remove methods.
3. Traverse hashtable
Hashtable traversal is similar to array traversal. However, the key-value pair is used to traverse elements in hashtable. The dictionaryentry type indicates the set of key-value pairs. For example:
Static void main (string [] ARGs)
{
Hashtable = new hashtable ();
Hashtable. Add ("ID", "hb001 ");
Hashtable. Add ("name", "TM ");
Hashtable. Add ("sex", "male ");
Console. writeline ("\ t key \ tvalue ");
Foreach (dictionaryentry dicentry in hashtable)
{
Console. writeline ("\ t" + dicentry. Key + "\ t" + dicentry. value );
}
Console. Readline ();
}
2014.9.20hashtable Overview