To expand on the example, consider the primitive type Integer, which is really the value
type System.Int32. It??™s one of the most basic types in the common language runtime (CLR).
You may or may not have noticed that it implements several interfaces: IComparable,
IFormattable, IConvertible, IComparable(Of Integer), and IEquatable(Of Integer).
IConvertible has 17 methods; however, none of them are part of the public contract of
System.Int32. If you want to call one of the IConvertible methods, you must first cast your
Int32 value type to an IConvertible. Of course, since interface-typed variables are of reference
type, this will box the Int32 value type.
Using Generics with Interfaces
We don??™t cover the topic of generics in detail until Chapter 12 because it??™s much easier to discuss
it after you??™ve learned more VB; but here are a couple of simple examples of using
generics with interfaces to suggest some of their possibilities.
Using a Generic Interface
Interfaces can be generic??”that is, they can provide one or more type parameters that are filled
by type arguments when the interface is used. Take a look at this example:
Option Strict Off
Interface IGeneric(Of T)
Sub SomeMethod(ByVal x As T)
End Interface
Class A
Implements IGeneric(Of Integer)
Public Sub SomeMethod(ByVal x As Integer) _
Implements IGeneric(Of Integer).
Pages:
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196