Section 9.4. Collection Interfaces


9.4. Collection Interfaces

The .NET Framework provides two sets of standard interfaces for enumerating and comparing collections: the traditional (nontype-safe) and the new generic type-safe collections. This book focuses only on the new, type-safe collection interfaces as these are far preferable.

You can declare an ICollection of any specific type by substituting the actual type (for example, int or string) for the generic type in the interface declaration (<T>).

C++ programmers note: C# generics are similar in syntax and usage to C++ templates. However, because the generic types are expanded to their specific type at runtime, the JIT compiler is able to share code among different instances, dramatically reducing the code bloat that you may see when using templates in C++.


The key generic collection interfaces are listed in Table 9-2.[3]

[3] For backward compatibility, C# also provides nongeneric interfaces (e.g., ICollection, IEnumerator), but they aren't considered here because they are obsolescent.

Table 9-2. Collection interfaces

Interface

Purpose

ICollection<T>

Base interface for generic collections.

IEnumerator<T> IEnumerable<T>

Enumerates through a collection using a foreach statement .

ICollection<T>

Implemented by all collections to provide the CopyTo( ) method as well as the Count, IsSynchronized, and SyncRoot properties.

IComparer<T> IComparable<T>

Compares two objects held in a collection so that the collection can be sorted.

IList<T>

Used by array-indexable collections.

IDictionary<K,V>

Used for key/value-based collections such as Dictionary.


9.4.1. The IEnumerable<T> Interface

You can support the foreach statement in ListBoxTest by implementing the IEnumerable<T> interface (see Example 9-11). IEnumerable has only one method, GetEnumerator( ), whose job is to return an implementation of IEnumerator<T>. The C# language provides special help in creating the enumerator, using the new keyword yield.

Example 9-11. Making a ListBox an enumerable class
#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace Enumerable {    public class ListBoxTest : IEnumerable<String>    {       private string[] strings;       private int ctr = 0;       // Enumerable classes can return an enumerator       public IEnumerator<string> GetEnumerator( )       {          foreach ( string s in strings )          {             yield return s;          }       }       // initialize the list box with strings       public ListBoxTest( params string[] initialStrings )       {          // allocate space for the strings          strings = new String[8];          // copy the strings passed in to the constructor          foreach ( string s in initialStrings )          {             strings[ctr++] = s;          }       }       // add a single string to the end of the list box       public void Add( string theString )       {          strings[ctr] = theString;          ctr++;       }       // allow array-like access       public string this[int index]       {          get          {             if ( index < 0 || index >= strings.Length )             {                // handle bad index             }             return strings[index];          }          set          {             strings[index] = value;          }       }       // publish how many strings you hold       public int GetNumEntries( )       {          return ctr;       }    }    public class Tester    {       static void Main( )       {          // create a new list box and initialize          ListBoxTest lbt =             new ListBoxTest( "Hello", "World" );          // add a few strings          lbt.Add( "Who" );          lbt.Add( "Is" );          lbt.Add( "John" );          lbt.Add( "Galt" );          // test the access          string subst = "Universe";          lbt[1] = subst;          // access all the strings          foreach ( string s in lbt )          {             Console.WriteLine( "Value: {0}", s );          }       }    } } Output: Value: Hello Value: Universe Value: Who Value: Is Value: John Value: Galt Value: Value:

The program begins in Main( ), creating a new ListBoxTest object and passing two strings to the constructor. When the object is created, an array of Strings is created with enough room for eight strings. Four more strings are added using the Add method, and the second string is updated, just as in the previous example.

The big change in this version of the program is that a foreach loop is called, retrieving each string in the listbox. The foreach loop automatically uses the IEnumerable<T> interface, invoking GetEnumerator( ).

The GetEnumerator method is declared to return an IEnumerator of string:

public IEnumerator<string> GetEnumerator( )

The implementation iterates through the array of strings, yielding each in turn:

foreach ( string s in strings ) {    yield return s; }

All the bookkeeping for keeping track of which element is next, resetting the iterator, and so forth, is provided for you by the framework.



Programming C#(c) Building. NET Applications with C#
Programming C#: Building .NET Applications with C#
ISBN: 0596006993
EAN: 2147483647
Year: 2003
Pages: 180
Authors: Jesse Liberty

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