Understanding Constraints
With constraints you can control Generics’ behavior, and both provide additional functionalities and limit the implementation to specific data types. Let’s begin by understanding constraints on methods.
Methods Constraints
Imagine you want the ability to compare two items within an array. To accomplish this you need to take advantage of the IComparable
interface, and because of this, you want to require that the type argument implements the IComparable
interface. The following code demonstrates this:
Notice how the As
clause in the method argument requires the type to implement the IComparable
interface. If the type ...
Get Visual Basic® 2010 Unleashed 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.