Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <T> denote in C# [duplicate]

Tags:

c#

.net

generics

I'm new to C# and directly diving into modifying some code for a project I received. However, I keep seeing code like this :

class SampleCollection<T> 

and I cannot make sense of what the

<T>  

means nor what it is called.

If anyone would care to help me just name what this concept is called, I can search it online. However, I'm clueless as of now.

like image 472
YD8877 Avatar asked Mar 25 '12 01:03

YD8877


People also ask

What does T stand for in programming?

T is a simple object-oriented programming language, modeled on Java. T supports only one primitive type, the integer type. T also supports reference types via the class Object, which is the root of the inheritance hierarchy. T supports only single-inheritance. T syntax is derived from Java syntax.

What is AT and T in c?

\t is used to give tab spaces in c programming. \ t prints out a tab which is an undefinable ammount of space that aligns the next section of output to a horizontal tab on the screen. For example: printf(“HELLO\tQUORA.”) ; Output: HELLO QUORA.

What is the meaning of Slash T?

Common Escape sequences t \t (Horizontal bar) – This command moves the cursor to control a couple of spaces (usually 4) to the right in the same line. \” (Double quotation mark)- This character is used to display the double quotation mark. \0 (Null character) – This character is used to represent the string's end.

What does '\ t mean in C++?

'\t' is the horizontal tab character. http://en.cppreference.com/w/cpp/language/escape.


2 Answers

It is a Generic Type Parameter.

A generic type parameter allows you to specify an arbitrary type T to a method at compile-time, without specifying a concrete type in the method or class declaration.

For example:

public T[] Reverse<T>(T[] array) {     var result = new T[array.Length];     int j=0;     for(int i=array.Length - 1; i>= 0; i--)     {         result[j] = array[i];         j++;     }     return result; } 

reverses the elements in an array. The key point here is that the array elements can be of any type, and the function will still work. You specify the type in the method call; type safety is still guaranteed.

So, to reverse an array of strings:

string[] array = new string[] { "1", "2", "3", "4", "5" }; var result = reverse(array); 

Will produce a string array in result of { "5", "4", "3", "2", "1" }

This has the same effect as if you had called an ordinary (non-generic) method that looks like this:

public string[] Reverse(string[] array) {     var result = new string[array.Length];     int j=0;     for(int i=array.Length - 1; i >= 0; i--)     {         result[j] = array[i];         j++;     }     return result; } 

The compiler sees that array contains strings, so it returns an array of strings. Type string is substituted for the T type parameter.


Generic type parameters can also be used to create generic classes. In the example you gave of a SampleCollection<T>, the T is a placeholder for an arbitrary type; it means that SampleCollection can represent a collection of objects, the type of which you specify when you create the collection.

So:

var collection = new SampleCollection<string>(); 

creates a collection that can hold strings. The Reverse method illustrated above, in a somewhat different form, can be used to reverse the collection's members.

like image 55
Robert Harvey Avatar answered Oct 02 '22 02:10

Robert Harvey


It is a generic type parameter, see Generics documentation.

T is not a reserved keyword. T, or any given name, means a type parameter. Check the following method (just as a simple example).

T GetDefault<T>() {     return default(T); } 

Note that the return type is T. With this method you can get the default value of any type by calling the method as:

GetDefault<int>(); // 0 GetDefault<string>(); // null GetDefault<DateTime>(); // 01/01/0001 00:00:00 GetDefault<TimeSpan>(); // 00:00:00 

.NET uses generics in collections, ... example:

List<int> integerList = new List<int>(); 

This way you will have a list that only accepts integers, because the class is instancited with the type T, in this case int, and the method that add elements is written as:

public class List<T> : ... {     public void Add(T item); } 

Some more information about generics.

You can limit the scope of the type T.

The following example only allows you to invoke the method with types that are classes:

void Foo<T>(T item) where T: class { } 

The following example only allows you to invoke the method with types that are Circle or inherit from it.

void Foo<T>(T item) where T: Circle { } 

And there is new() that says you can create an instance of T if it has a parameterless constructor. In the following example T will be treated as Circle, you get intellisense...

void Foo<T>(T item) where T: Circle, new() {     T newCircle = new T(); } 

As T is a type parameter, you can get the object Type from it. With the Type you can use reflection...

void Foo<T>(T item) where T: class {     Type type = typeof(T); } 

As a more complex example, check the signature of ToDictionary or any other Linq method.

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector); 

There isn't a T, however there is TKey and TSource. It is recommended that you always name type parameters with the prefix T as shown above.

You could name TSomethingFoo if you want to.

like image 44
BrunoLM Avatar answered Oct 02 '22 02:10

BrunoLM