Csharp/CSharp Tutorial/Data Structure/IDictionaryEnumerator

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Demonstrate IDictionaryEnumerator

<source lang="csharp">using System; using System.Collections;

class MainClass {

 public static void Main() { 
   // Create a hash table. 
   Hashtable ht = new Hashtable(); 
    
   // Add elements to the table 
   ht.Add("A", "3"); 
   ht.Add("B", "9"); 
   ht.Add("C", "3"); 
   ht.Add("D", "7"); 

   // Demonstrate enumerator 
   IDictionaryEnumerator etr = ht.GetEnumerator(); 
   Console.WriteLine("Display info using Entry."); 
   while(etr.MoveNext())  
    Console.WriteLine(etr.Entry.Key + ": " +  
                      etr.Entry.Value); 

   Console.WriteLine(); 

   Console.WriteLine("Display info using Key and Value directly."); 
   etr.Reset(); 
   while(etr.MoveNext())  
    Console.WriteLine(etr.Key + ": " +  
                      etr.Value); 
    
 } 

}</source>

Display info using Entry.
A: 3
B: 9
C: 3
D: 7
Display info using Key and Value directly.
A: 3
B: 9
C: 3
D: 7