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