4.10 Enumerating Collections with IEnumerator

 <  Day Day Up  >  

4.10 Enumerating Collections with IEnumerator

You want to create a function that can enumerate the items of a collection regardless of the collection data type.


Technique

The IEnumerator interface allows you to enumerate the contents of collections that implement that interface, which you can retrieve by calling the GetEnumerator method on an instance of a collection. To begin enumeration, call the MoveNext method from the IEnumerator interface. Next, create a loop that breaks when MoveNext returns false . Within the loop body, retrieve the current value by calling the ToString method defined on the Current property. However, if the collection is a dictionary-based collection, check the type of the Current property; if it is a DictionaryEntry type, you have to cast the Current property to a DictionaryEntry object and access the Value property, as shown in Listing 4.4.

Listing 4.4 Printing Collections That Implement the IEnumerator Interface
 using System; using System.Collections; namespace _9_IEnumerator {     class Class1     {         [STAThread]         static void Main(string[] args)         {             int[] intArr = {1, 2, 3, 4, 5};             ArrayList al = new ArrayList( intArr );             bool[] baVals = { true, true, false, false, true };             BitArray ba = new BitArray( baVals );             Hashtable ht = new Hashtable();             ht.Add( 1, "one" );             ht.Add( 2, "two" );             ht.Add( 3, "three" );             ht.Add( 4, "four" );             ht.Add( 5, "five" );             PrintCollection( "Integer Array", intArr.GetEnumerator() );             PrintCollection( "ArrayList", al.GetEnumerator() );             PrintCollection( "BitArray", ba.GetEnumerator() );             PrintCollection( "Hashtable", ht.GetEnumerator() );         }         static void PrintCollection( string name, IEnumerator collEnum )         {             Console.WriteLine( collEnum.GetType().ToString() );             Console.WriteLine( name );             while( collEnum.MoveNext() )             {                 if( collEnum.Current.GetType() ==                     Type.GetType("System.Collections.DictionaryEntry") )                 {                     DictionaryEntry di = (DictionaryEntry) collEnum.Current;                     Console.Write( di.Value.ToString() + " " );                 }                 else                 {                     Console.Write( collEnum.Current.ToString() + " " );                 }             }             Console.WriteLine("\n");         }     } } 

Comments

One of the major advantages of object-oriented programming lies in the fact that objects can share some of the same characteristics, thereby allowing similar access to their data. Collections within the .NET Framework are no exception. Each collection implements two known interfaces, ICollection and IEnumerable , in addition to the interface that is implemented based on the type of collection ( IList for list-based collections and IDictionary for dictionary-based collections like the Hashtable ). Because collections implement these interfaces, you can create generic methods that can operate on differing collection types without having to create methods for each type of collection.

IEnumerable contains only a single method named GetEnumerator . This method returns a different interface named IEnumerator . Because collections implement the IEnumerable interface, you can retrieve the enumerator for each collection and use methods that allow you to traverse the items in the collection one at a time.

Although synchronization isn't covered until a later chapter, it's important to note that an enumerator is valid only if the collection never changes. In other words, if you are enumerating a collection and another thread changes a value in the collection, the enumerator is invalid and any calls to MoveNext or Reset throw an exception. You should therefore either lock the collection or handle the exception so you can recover should this event happen. Chapter 6, "Exceptions and Error Handling," discusses exceptions.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net