w0051977
w0051977

Reputation: 15817

Class (of T) - What is T?

I understand the basics of generics i.e. it is validation and removed by the compiler. I see plenty of code like this:

Public Class Person(Of T)

End Class

I do not fully understand what T is. I would expect something like Of Person or Of Order as T does not exist in my problem domain.

I believe what this is saying is that all references to T in the class must be the class instance, but I am not sure.

I have spent some time Googling this and I have even reread the Generics section of an Object Oriented text book I own. I have not yet found an answer.

Upvotes: 0

Views: 8286

Answers (3)

supercat
supercat

Reputation: 81217

It is a parameter; although T is the most commonly used name, other names would be possible. If one considers a method definition:

Sub Foo(Bar As Integer)

the T in a type definition plays much the same role as the Bar in a method definition. The most notable thing about generic type parameters is unlike parameter definitions which in the sane (Option Strict Off) dialect of VB must always specify a type [the As Integer], generic type parameters can specify a constraint type Class Foo(Of T As Control) but are not required to do so. Further, even when a constraint type is specified, T is not an instance of Control, but rather a type which is required to derive from Control.

Upvotes: 1

It is just a placeholder to signify that the Type will be defined at runtime:

Public Class Elements(Of T)
    Private mList as List(of T)

...
End Class

...

Friend El as New Elements(of String)

My code creates an El object and passes the T (String) to the Class which I can use to define the inner workings. It is a way to use typed objects as opposed to inheriting numerous classes of each likely datatype, or just declaring such things as Object.

Sometimes you'll see 2 of them:

Foo(Of T, Of TT)

It just means there are 2 Types to be defined later. There is nothing special about T - it is just convention, Of is the keyword.

Upvotes: 0

Plasmarob
Plasmarob

Reputation: 1391

T stands for Type. (in C++ it's template, though)

It means that the method is static, or generic.

The T itself indicates that the method is generic.

If you understand generics, it should make sense to you that it's kind of a placeholder. (if i understand correctly)

This should explain why

Public Class Person(Of T)

End Class

is like Public Class (of ________ (type) ______)

VB.net: What is static T (C#) in VB.net?

http://msdn.microsoft.com/en-us/library/ms379564%28VS.80%29.aspx

Upvotes: 0

Related Questions