.NET Framework Collection Class Design


A collection is a collection. Although this may sound like an odd statement and not make much sense, it does hold some truth. If you were to take a collection of objects internally organized a certain way and another collection of objects internally organized in a different way, would you want different ways of inserting, removing, or retrieving them? For instance, if you have a basket of oranges and a shopping cart of oranges, why should you have to learn two different ways to work with those containers given that they serve the same purpose to collect oranges to be retrieved later?

The collection classes within the .NET Framework are designed in such a way that manipulating the objects each collection contains is similar and may be even the same among the different collection types. This is achieved through the inheritance of collection interfaces that are defined within the framework itself. An interface is a public declaration or contract that an object declares it implements so that clients who use the interface on that object know what to expect. You will learn more about interfaces in Hour 17, "Interfaces."

The main interface implemented by the framework collection classes is the ICollection interface. This interface contains three properties and one method. The Count property returns the number of elements the collection contains. The IsSynchronized property returns whether or not the elements of a collection are synchronized for multithread access. In order to use the synchronization features of a collection, you can access the SyncRoot property, which returns a synchronized object you can use to access the current elements. The single function within the ICollection interface is the CopyTo function. This function copies the current elements of the collection into a single-dimension array, starting at a given index into that array.

The ICollection Interface

ICollection is a very generic interface and doesn't provide much functionality for a client using the collection. Therefore, the .NET Framework provides two more interfaces that derive from the ICollection interface that many collections implement. The IList interface supports collections that are ordered and can be accessed with an index value. One of the more prominent collection classes that implement this interface is the System::Array class. The other interface that implements ICollection and is also used by many other collection classes is the IDictionary interface. A collection that implements IDictionary represents elements as key/value pairs. In other words, rather than using some sort of ordered index such as an integer, the keys used in IDictionary can be any .NET object. One of the most prominent collection classes that implements this interface is the Hashtable class.

The Enumeration Interfaces

The IEnumerable interface, like the ICollection interface, is a very generic interface. In fact, there is only one method: GetEnumerator. The IEnumerable interface is responsible for passing a client an interface pointer to the main interface responsible for enumerating through the different elements within the collection. The interface returned by the GetEnumerator function is IEnumerator.

The IEnumerator interface, as mentioned, is responsible for enumerating through all the elements of the collection and returning the element values back to the client using that collection. The IEnumerator interface contains two key functions and one property. The Current property is used to obtain the current element that the enumerator is currently accessing. Once the value has been retrieved, a client calls the MoveNext method to advance the enumerator to the next element. If this method returns a false value, no more elements are left in the collection. In order to reset the enumerator to the beginning of the collection, the IEnumerator interface contains a Reset function.

The IEnumerator interface, in conjunction with IEnumerable, is also defined so that other .NET languages can enumerate through collections using foreach statements. Because collection enumeration is quite a common programming task, C# .NET and VB .NET contain special keyword programming constructs that can be easily used to walk through the elements of a collection. An example of enumerating through a collection that implements the enumeration interfaces using C# is shown in the following code block:

 static void Main(string? args) {     // Declare an Array object:     ArrayList alNames = new ArrayList();     // Add entries using the Add() method:     alNames.Add("Doug")     alNames.Add("Heather");     alNames.Add("Kevin");     alNames.Add("Julie");     alNames.Add("Daniel");     alNames.Add("Lynne");     // Display contents:     foreach (string curName in alNames)     {         Console.WriteLine( curName );     } } 


Sams Teach Yourself Visual C++. NET in 24 Hours
Sams Teach Yourself Visual C++.NET in 24 Hours
ISBN: 0672323230
EAN: 2147483647
Year: 2002
Pages: 237

Similar book on Amazon

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