Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do generics often use T?

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>(){ ... }
like image 833
Sorskoot Avatar asked Dec 17 '08 08:12

Sorskoot


People also ask

What letters do you use for generics?

Most likely, the default letter to use is T because it stands for "Type", then just like in mathematics it's conventional to use consecutive letters for multiple variables representing the same kinds of thing, so S , T , U , V are common.

Why do we use generics?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

What is the exact meaning of generic?

A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

What are the advantages of generic programming?

Generic Programming enables the programmer to write a general algorithm which will work with all data types. It eliminates the need to create different algorithms if the data type is an integer, string or a character. Once written it can be used for multiple times and cases.


2 Answers

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.

like image 166
Vilx- Avatar answered Sep 19 '22 12:09

Vilx-


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

like image 38
yesraaj Avatar answered Sep 20 '22 12:09

yesraaj