4.6. Consuming Generic Classes

Okay, you've had a good long look at what goes into defining a generic class. Now, it's time to explore those rules that govern constructed types. First, it should be clear by now that a constructed type shares all the freedoms as any other type and can be placed in any syntactical context that would be used for non-generic types. Once you marry a type argument to the open type, they are conceptually "merged" to form a specific concrete type. The sooner you're comfortable with that notion, the sooner you'll begin to view constructed types on equal ground with Strings and Integers (actually, given my bias, I would tend to view them as generally superior to these types on the sheer merit of their constitution).

A number of variations exist on how you might declare a constructed type. The simplest variety is what is considered a closed constructed type. It's simple because the type arguments supplied are of simple, concrete types. Open constructed types use a type parameter as part of their declaration that is supplied by the surrounding generic class. These two types are likely familiar by now. However, Chapter 1, "Generics 101," provides examples if you want a more detailed explanation.

In addition to accepting all the primitive types as type arguments, a constructed type can also be created using other constructed types as type arguments. For example:

[VB code] Dim myGenericType1 as New MyType(Of List(Of Integer)) Dim myGenericType2 as New MyType2(Of ...

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.