Free mag vol1 | Page 399

CHAPTER 9  COLLECTIONS AND GENERICS } if (this.CarID < obj.CarID) return -1; else return 0; } Here, you do not need to check whether the incoming parameter is a Car because it can only be a Car! If someone were to pass in an incompatible data type, you would get a compile-time error. Now that you have a better handle on how to interact with generic items, as well as the role of type parameters (a.k.a. placeholders), you’re ready to examine the classes and interfaces of the System.Collections.Generic namespace. The System.Collections.Generic Namespace When you are building a .NET application and need a way to manage in-memory data, the classes of System.Collections.Generic will most likely fit the bill. At the opening of this chapter, I briefly mentioned some of the core nongeneric interfaces implemented by the nongeneric collection classes. Not too surprisingly, the System.Collections.Generic namespace defines generic replacements for many of them. In fact, you can find a number of the generic interfaces that extend their nongeneric counterparts. This might seem odd; however, by doing so, implementing classes will also support the legacy functionally found in their nongeneric siblings. For example, IEnumerable extends IEnumerable. Table 9-4 documents the core generic interfaces you’ll encounter when working with the generic collection classes. Table 9-4. Key Interfaces Supported by Classes of System.Collections.Generic 338 System.Collections.Generic Interface Meaning in Life ICollection Defines general characteristics (e.g., size, enumeration, and thread safety) for all generic collection types. IComparer Defines a way to compare to objects. IDictionary Allows a generic collection object to represent its contents using key/value pairs. IEnumerable Returns the IEnumerator interface for a given object. IEnumerator Enables foreach-style iteration over a generic collection. IList Provides behavior to add, remove, and index items in a sequential list of objects. ISet Provides the base interface for the abstraction of sets.