Chapter 6. Generics

One of the things developers often need to do is create new types for their programs. Early attempts at type creation led to user-defined types, or the use of the VB Structure statement. Another approach is to use classes and objects to create new types. Ever since the release of the .NET Framework 2.0, another approach is to use generics.

Generics refers to the technology built into the .NET Framework 3.5 (and the .NET Framework versions 2.0 and 3.0) that enables you to define a code template and then declare variables using that template. The template defines the operations that the new type can perform; and when you declare a variable based on the template, you are creating a new type. The benefit of generics over structures or objects is that a generic template makes it easier for your new types to be strongly typed. Generics also make it easier to reuse the template code in different scenarios.

The primary motivation for adding generics to .NET was to enable the creation of strongly typed collection types. Because generic collection types are strongly typed, they are significantly faster than the previous inheritance-based collection model. Anywhere you presently use collection classes in your code, you should consider revising that code to use generic collection types instead.

Visual Basic 2008 allows not only the use of preexisting generics, but also the creation of your own generic templates. Because the technology to support generics was created primarily ...

Get Professional Visual Basic® 2008 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.