2.3. A Basic Example

Type safety is likely a term you happened upon quite frequently in your travels as an object-oriented programmer. And, for you, the concept may already be crystallized. That said, I want to be sure we're on equal footing before examining some of the broader issues surrounding type safety and generics. So, to establish some common ground, let's look at a simple example that provides a very basic example of the importance of type safety.

The example constructed here consists of an object hierarchy with a Person class at the root and two descendant classes, Customer and Employee. The Person class provides an abstraction of those attributes that are common to every person. In this example, these shared attributes are represented by the Id, Name, and Status properties of the Person class.

The Customer and Employee classes also add their own specializations and behavior. Specifically, each of these classes also has a one-to-many relationship with another class. A Customer is associated with one or more Orders and an Employee contains references to one or more "child" Employee objects that represent those employees that are managed by a specific person.

Now, in working with these Customer and Employee objects, assume you've identified several places in your code that are providing general-purpose handling of Person objects. To further promote this generality, you've decided you also would like to allow clients of your Person class to access the items associated with ...

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.