Understanding Constraints
With constraints, you can control the behavior of generics and 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:
Public Function CompareItems(Of T As IComparable)(sourceArray() As T, index1 As Integer, index2 ...
Get Visual Basic 2015 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.