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>(){ ... }
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.
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.
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.
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.
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.
T for Type, as like you said everything works fine.But putting T in that place remind you that is of generic type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With