Several ways of Hashtable data traversal

Source: Internet
Author: User

Hashtable is called a key-value pair in the collection, and the type of each element is dictionaryentry, because the Hashtable object's key and value are object types, which determines that it can put any type of data,

Below I'll put a few objects in the Hashtable object that define a class.

The following classes are created:

//CodeStatic voidMain (string[] args) {Person Person1=NewPerson (); Person1. Age= the; Person1. Name="Jacky"; Person1. Email="[email protected]"; Person Person2=NewPerson (); Person2. Age= at; Person2. Name="Ajay"; Person2. Email="[email protected]"; Person Person3=NewPerson (); Person3. Age= A; Person3. Name="Bill"; Person3. Email="[email protected]"; Person Person4=NewPerson (); Person4. Age= at; Person4. Name="Gace"; Person4. Email="[email protected]"; Person Person5=NewPerson (); Person5. Age= $; Person5. Name="Jim"; Person5. Email="[email protected]"; Hashtable HT=NewHashtable (); Ht. ADD ("1", Person1); Ht. ADD ("2", Person2); Ht. ADD ("3", Person3); Ht. ADD ("4", Person4); Ht. ADD ("5", PERSON5); Console.WriteLine ("Please enter the user name of your query:"); stringStrName =Console.ReadLine (); //The first of these methods    foreach(stringIteminchht. Keys) {person P=(person) Ht[item]; if(StrName = =p.name) {Console.WriteLine ("the results after the query are:"+ P.name +"\ t"+ P.email +"\ t"+p.age); }    }    //The second method of    foreach(Person iteminchht. Values) {if(item. Name = =strName) {Console.WriteLine ("the results after the query are:"+ Item. Name +"\ t"+ Item. Email +"\ t"+item.        Age); }    }    //The third method of    foreach(DictionaryEntry IteminchHT) {        if(StrName = =(person) item. Value). Name) {Console.WriteLine ("the results after the query are:"+ (person) item. Value). Name +"\ t"+ (person) item. Value)."\ t"+(person) item. Value).        Age); }    }    //Fourth MethodIDictionaryEnumerator ID =ht.    GetEnumerator ();  while(ID. MoveNext ()) {person P=(person) ht[id.        Key]; if(P.name = =strName) {Console.WriteLine ("the results after the query are:"+ P.name +"\ t"+ P.email +"\ t"+p.age); }    }}

Several ways of Hashtable data traversal

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.