Appendix M. Generics
This appendix summarizes generic classes, extensions, and methods. Example program GenericExamples, which is available for download on the book's web site, demonstrates each of these.
The final section in this appendix describes items that you cannot make generic.
GENERIC CLASSES
The syntax for declaring a generic class is as follows:
[attribute_list] [Partial] [accessibility] [Shadows] [inheritance] _ Class name [(Oftype_list)] [Inheritsparent_class] [Implementsinterface]statementsEnd Class
All of these parts of the declaration are the same as those used by a normal (non-generic) class. See Chapter 26, "Classes and Structures," and Appendix K for information about non-generic classes.
The key to a generic class is the (Of type_list) clause. Here, type_list is a list of data types separated by commas that form the generic's parameter types. Each type can be optionally followed by the keyword As and a list of constraints that the corresponding type must satisfy. The constraint list can contain any number of interfaces and, at most, one class. It can also contain the New keyword to indicate that the corresponding type must provide an empty constructor. If a constraint list contains more than one item, the list must be surrounded by braces.
The following code defines the generic MyGeneric class. It takes three type parameters. The first is named Type1 within the generic's code and has no constraints. The second type, named Type2, must satisfy the IComparable interface. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access