itemtype As {New} in VB.net

Can a VB.net expert explain to me about itemtype in VB.net? For example I am seeing the following class declaration. I searched online, and in MSDN, but nothing meaningful came back.

Public Class Singleton(Of itemtype As {New})

Public Sub New()
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
It's a generic type definition with a constraint.  In this particular instance, itemType is a placeholder for a type to be defined later.  That type *must* have a parameterless constructor defined because of the {New} constraint.  Take for example:
Imports System.Runtime.CompilerServices
Imports EE_Q29101439

Module Module1

	Sub Main()
		Dim charlie = New Person With {.ID = 1, .Name = "Charlie", .Birthdate = DateTime.Now.AddYears(-9)}
		Dim spot = New Animal(1) With {.Name = "Spot", .Birthdate = DateTime.Now.AddYears(-3)}
	End Sub

End Module

Class Person
	Implements IBirthdate
	Dim _birthdate As DateTime

	Public Property ID() As Integer
	Public Property Name() As String
	Property Birthdate As DateTime Implements IBirthdate.Birthdate
		Get
			Return _birthdate
		End Get
		Set(value As DateTime)
			_birthdate = value
		End Set
	End Property
End Class

Class Animal
	Implements IBirthdate
	Dim _birthdate As DateTime

	Public Property ID() As Integer
	Public Property Name() As String
	Property Birthdate As DateTime Implements IBirthdate.Birthdate
		Get
			Return _birthdate
		End Get
		Set(value As DateTime)
			_birthdate = value
		End Set
	End Property

	Public Sub New(id As Integer)
		Me.ID = id
	End Sub
End Class

Interface IBirthdate
	Property Birthdate() As DateTime
End Interface

Module Extensions
	<Extension>
	Public Function GetAge(Of T As {New, IBirthdate})(source As T) As String
		Return $"{DateTime.Now.Year - source.Birthdate.Year} years old"
	End Function
End Module

Open in new window

GetAge is an extension method that accepts a parameter; *source* of type T, the type defined by T must Implement an Interface; *IBirhtdate* and have a parameterless constructer defined.  Here I have defined two classes, Person and Animal.  By default a class with no constructors defined has a public parameterless construtor.  So in this case, Person has a parameterless constructor, but what about Animal?

Well let's test and see.  If we want to get the age of the person we can simply do the following:Capture.PNGIf however, we try to use the same method on the animal instance:Capture.PNGThe method is not available.  This is because in order for Animal to have a parameterless constructor we must define one since we defined a parameterized construtor.
Class Animal
	Implements IBirthdate
	Dim _birthdate As DateTime

	Public Property ID() As Integer
	Public Property Name() As String
	Property Birthdate As DateTime Implements IBirthdate.Birthdate
		Get
			Return _birthdate
		End Get
		Set(value As DateTime)
			_birthdate = value
		End Set
	End Property

	Public Sub New()

	End Sub

	Public Sub New(id As Integer)
		Me.ID = id
	End Sub
End Class

Open in new window

Capture.PNGThere, thats better.  Here is the full code:
Imports System.Runtime.CompilerServices
Imports EE_Q29101439

Module Module1

	Sub Main()
		Dim charlie = New Person With {.ID = 1, .Name = "Charlie", .Birthdate = DateTime.Now.AddYears(-9)}
		Dim spot = New Animal(1) With {.Name = "Spot", .Birthdate = DateTime.Now.AddYears(-3)}
		Console.WriteLine($"Charlie is a boy who is {charlie.GetAge()}.")
		Console.WriteLine($"Charlie has a dog named {spot.Name}.  {spot.Name} is {spot.GetAge()}.")
		Console.ReadLine()
	End Sub

End Module

Class Person
	Implements IBirthdate
	Dim _birthdate As DateTime

	Public Property ID() As Integer
	Public Property Name() As String
	Property Birthdate As DateTime Implements IBirthdate.Birthdate
		Get
			Return _birthdate
		End Get
		Set(value As DateTime)
			_birthdate = value
		End Set
	End Property
End Class

Class Animal
	Implements IBirthdate
	Dim _birthdate As DateTime

	Public Property ID() As Integer
	Public Property Name() As String
	Property Birthdate As DateTime Implements IBirthdate.Birthdate
		Get
			Return _birthdate
		End Get
		Set(value As DateTime)
			_birthdate = value
		End Set
	End Property

	Public Sub New()

	End Sub

	Public Sub New(id As Integer)
		Me.ID = id
	End Sub
End Class

Interface IBirthdate
	Property Birthdate() As DateTime
End Interface

Module Extensions
	<Extension>
	Public Function GetAge(Of T As {New, IBirthdate})(source As T) As String
		Return $"{DateTime.Now.Year - source.Birthdate.Year} years old"
	End Function
End Module

Open in new window

Which produces the following output -Capture.PNG
You can read more about Generic Types here:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/generic-types

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.