C # Hashtable

Source: Internet
Author: User

Hash table (Hashtable)

In the. NET framework, Hashtable is a container provided by the System.Collections namespace that handles and behaves like Key-value key-value pairs, where key is typically used for quick lookups, while key is case-sensitive ; value is used to store the value corresponding to key.

Key-value Key-value pairs in Hashtable are all object types, so Hashtable can support any type of Key-value key-value pair.

Creating an Hashtable instance
New Hashtable ();
Add Key-value key value pair
Ht. ADD ("A","1"); Ht. ADD ("B","2"); Ht. ADD ("C","3"); Ht. ADD ("D","4");
Traverse
//traversing a hash table can only be traversed with foreach, because Hashtable cannot be accessed with an index//Traverse Keyforeach(Object Iteminchht. Keys) {Console.WriteLine (string) item);}//Traverse Valueforeach(Object Iteminchht. Values) {Console.WriteLine (string) item);}//Traverseforeach(DictionaryEntry deinchHT) {Console.WriteLine ("Key: {0}; Value: {1}.", DE. Key, DE. Value);}//Traversal hash Table (results are the same as above)IDictionaryEnumerator Myenumerator =ht. GetEnumerator (); while(Myenumerator.movenext ()) {Console.WriteLine ("Key: {0}; Value: {1}.", Myenumerator.key, Ht[myenumerator.key]);}

Hash table Sort
New ArrayList (HT. Keys); list. Sort (); foreach (string in list) {    Console.WriteLine ("Key: {0}; Value: {1}. " , Key, Ht[key]);}

Assign value
ht["A" " Hello ";
Contains determine if a hash table contains a specific key
// The return value is True or false if (HT. Contains ("A")) {    Console.WriteLine ("Key: {0}; Value: {1}. " " A ", ht["A"]);}
Remove removes a Key-value key-value pair
Ht. Remove ("C");
Clear removes all elements
Ht. Clear ();
Clone replication
 as hashtable;ht2["A" " World "; ht[" B " " Good Night " ; Ht. ADD ("E""5");

As you can see, after cloning, two Hashtable are not affected by each other.

CopyTo Copy the Hashtable middle key or list of values into the array
string[] Mytargetarray =Newstring[ the];mytargetarray[0] =" the"; mytargetarray[1] ="Quick"; mytargetarray[2] ="Brown"; mytargetarray[3] ="Fox"; mytargetarray[4] ="jumped"; mytargetarray[5] =" Over"; mytargetarray[6] =" the"; mytargetarray[7] ="Lazy"; mytargetarray[8] ="Dog"; Ht. Keys.copyto (Mytargetarray,6);//The assignment key is in the array and is overwritten from the 7th position. Ht. Values.copyto (Mytargetarray,Ten);//assigns values to the array, overwriting from the 7th bit. 

HT is not executed. Keys.copyto (Mytargetarray, 6) ago

Perform HT. Keys.copyto (Mytargetarray, 6) after

Perform HT. Values.copyto (Mytargetarray, 10) after

Full code

usingSystem;usingSystem.Collections;//This namespace must be introduced when using Hashtablenamespaceconsoletest{classHashtabletest { Public Static voidHashtabletestmain () {//Create a Hashtable instanceHashtable HT =NewHashtable (); //adding Key-value Key-value pairsHt. ADD ("A","1"); Ht. ADD ("B","2"); Ht. ADD ("C","3"); Ht. ADD ("D","4"); //traversing a hash table can only be traversed with foreach, because Hashtable cannot be accessed with an index//Traverse Key            foreach(Object Iteminchht. Keys) {Console.WriteLine ("Key: {0};", (string) item); }            //Traverse Value            foreach(Object Iteminchht. Values) {Console.WriteLine ("Value: {0};", (string) item); }            //Traverse            foreach(DictionaryEntry deinchHT) {Console.WriteLine ("Key: {0}; Value: {1}.", DE. Key, DE.            Value); }            //Hash Table SortArrayList list =NewArrayList (HT.            Keys); List.            Sort (); foreach(stringKeyinchlist) {Console.WriteLine ("Key: {0}; Value: {1}.", Key, Ht[key]); }            //Assign Valueht["A"] ="Hello"; //determines whether the hash table contains a specific key with a return value of TRUE or false            if(HT. Contains ("A") {Console.WriteLine ("Key: {0}; Value: {1}.","A", ht["A"]); }            //Remove a Key-value key-value pairHt. Remove ("C"); //traversing a hash tableIDictionaryEnumerator Myenumerator =ht.            GetEnumerator ();  while(Myenumerator.movenext ()) {Console.WriteLine ("Key: {0}; Value: {1}.", Myenumerator.key, Ht[myenumerator.key]); }            //CopyHashtable ht2 = ht. Clone () asHashtable; ht2["A"] ="World"; ht["B"] ="Good Night"; Ht. ADD ("E","5"); //methods to copy the Hashtable middle key or list of values to an arraystring[] Mytargetarray =Newstring[ the]; mytargetarray[0] =" the"; mytargetarray[1] ="Quick"; mytargetarray[2] ="Brown"; mytargetarray[3] ="Fox"; mytargetarray[4] ="jumped"; mytargetarray[5] =" Over"; mytargetarray[6] =" the"; mytargetarray[7] ="Lazy"; mytargetarray[8] ="Dog"; Ht. Keys.copyto (Mytargetarray,6);//The assignment key is in the array and is overwritten from the 7th position. Ht. Values.copyto (Mytargetarray,Ten);//assigns values to the array, overwriting from the 7th bit. //Remove all elementsht.            Clear ();        Console.readkey (); }    }}
Hashtable complete Code

C # Hashtable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.