What does <T> mean in . NET?

Asked

Viewed 3,199 times

6

I’m working on a project and I saw a lot of code like this

public class ExemploCollection<T> { ... }

And I don’t understand what this <T> means.

What’s the name of it, what am I looking for? How does it work? What does it do?

  • 8

    I find it a little honest to remove a post by receiving votes against and replacing them later. I think it’s nice to try to maintain the seriousness of the commutation.

  • 1

    @Don to be fair, the quality of the question in this replay is far superior to that of the first post (in other words, now there is a credible question, while the previous one seemed to have been made with the sole purpose of justifying the answer)

  • 1

    @mgibsonbr In this case perhaps it would be better to edit the post, I don’t want to go into the subject anymore, it was just my personal vision.

  • @Don Already I am indifferent between edit or delete/reset. The only thing that matters to me is the quality of the last revision. After all, those who voted against voted in the publication of poor quality, and not all will reevaluate the vote after the issue. Having a good/reasonable quality publication carrying the "legacy" of a poor quality does not benefit anyone, IMHO.

  • 1

    Was this question only asked to post the answer right away? Or is the OS bug and shows the same person and answer in the same minute?

  • I partially agree with the 2, in theory I should update the question to improve it, but we know that in practice it does not work well thus, the negative votes will not be revised, and the pertinent question will be ignored.

  • @Mayogax this is ok, and even encouraged. See more in http://answall.com/help/self-answer

Show 2 more comments

1 answer

8


The context name of this is Generics, see the documentation.

T is not a reserved word. T, or any other given name means "type parameter". See the following method:

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

Note that the guy return method is T. With this method I can get the default value of any type this way:

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

The . NET uses Generics in collections of objects, such as List<T>

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

This way you will have a list that will only accept integers, because the class is instantiated with type "T", in this case int and the method that adds elements only accepts the type given when instantiating the class:

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

How do we have a List<int>, the method Add will only accept whole.

In Generics it is possible to limit the scope of the type T.

The following example only allows the use of the method with types that are classes

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

The following example only allows the use of the method with types that are classes of the type Circulo or inherited from Circulo.

void FazAlgo<T>(T item) where T: Circulo
{
}

And there’s also the new(), that if used in this way, one can create a circle instance:

void FazAlgo<T>(T item) where T: Circulo, new()
{
    T novoCirculo = new T();
}

Like T is a type parameter, it is possible to get the object Type. And with the object Type it is possible to do several things using Reflection.

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

In a more complex example, to demonstrate what is possible to do with Generics see the method declaration ToDictionary, or any other Linq method.

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

See that there is no T. As I said before T is not a reserved word. It is always a practice to name the type parameters with the prefix T, as seen in the example above TKey and TSource.

You may appoint TFoo if you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.