views:

548

answers:

8

Is there any reason for the use of 'T' in generics? Is it some kind of abbreviation? As far as I know, everything works. For example

public G Say<G>(){ ... }

or even

public Hello Say<Hello>(){ ... }
+8  A: 

T for Type, as like you said everything works fine.But putting T in that place remind you that is of generic type.

yesraaj
+4  A: 

oh, I would have thought T for Thing :)

Joel in Gö
I though it was T for "This totally rocks!"
Robert Gould
I thought it was T for 2 at 4.
Daniel Earwicker
+1 and I thought it was for *it.
kenny
...and S for Something Else
Joel in Gö
+6  A: 

It's just a shorthand like I is conventionally used for interfaces in .NET, and in other environments C is sometimes used for classes (Delphi uses this, IIRC).

Generally "T" on its own means "the single type parameter in this context" and if you have multiple type parameters, they get a T prefix, e.g. Dictionary<TKey, TValue>. It just makes it obvious when you're reading the code that it's a type parameter rather than a specific concrete type.

Jon Skeet
A: 

T for Type

Also, E is used for Element that's very common too. You're right G also works.

OscarRyz
+25  A: 

T is for Type. But it's really just a tradition and there is nothing to prevent you from using other names. For example, generic dictionaries use <TKey, TValue>.

There is also a Microsoft guideline that recommends using the letter T if you have a single type parameter, and prefix descriptive parameter names with T if you have more than one. Doing so will provide a more consistent feel across your code.

Vilx-
+2  A: 

There might also be a bit of tradition too as C++ templates use T most of the time, and generics are similar in function to C++'s templates, when used for generic programming.

Robert Gould
+1  A: 

If your generic type represents something special, you can make it more precise.. Usually prefixing it with T : IRepository<TEntity>, SomeCollection<TItem, TComparer> ..

Think Before Coding
A: 

I thought that was T for Template because it first appears in C++.

Nicolas Dorier