Tutorial by Examples

A generic type is created to adapt so that the same functionallity can be accessible for different data types. Public Class SomeClass(Of T) Public Sub doSomething(newItem As T) Dim tempItem As T ' Insert code that processes an item of data type t. End Sub End Class ...
By creating an instance of the same class with a different type given, the interface of the class changes depending on the given type. Dim theStringClass As New SomeClass(Of String) Dim theIntegerClass As New SomeClass(Of Integer)
A generic class is a class who adapts to a later-given type so that the same functionality can be offered to different types. In this basic example a generic class is created. It has a sub who uses the generic type T. While programming this class, we don't know the type of T. In this case T has ...
In this example there are 2 instances created of the SomeClass Class. Depending on the type given the 2 instances have a different interface: Dim theStringClass As New SomeClass(Of String) Dim theIntegerClass As New SomeClass(Of Integer) The most famous generic class is List(of )
The possible types passed to a new instance of SomeClass must inherit SomeBaseClass. This can also be an interface. The characteristics of SomeBaseClass are accessible within this class definition. Public Class SomeClass(Of T As SomeBaseClass) Public Sub DoSomething(newItem As T) new...
Creating a new intance of a generic type can be done/checed at compile time. Public Class SomeClass(Of T As {New}) Public Function GetInstance() As T Return New T End Function End Class Or with limited types: Public Class SomeClass(Of T As {New, SomeBaseClass}) Public F...

Page 1 of 1