Define a generic method (VB.NET)

A reminder of how the VB.NET syntax is for defining a generic method.
Just a sample.

The New constraint must be the first one, if needed.
The interface constraint is to be able to use properties and methods on the generic type.
It could be a base class also. The base class should then be placed before the interfaces.

Imports System.Collections.Generic 'List(Of

        Public Sub MyGenericSub(Of T As {New, IImplementedByT})(ByRef MyList As List(Of T))
            Dim myObj As New T
            myObj.AMethodOnIImplementedByT()

And some C#:
http://msdn.microsoft.com/en-us/library/d5x73970.aspx
The New constraint must be the last one, if needed.

public bool MyMethod<T>(T t) where T : IMyInterface, new() { }
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: