What does "T" mean in C #?

I have a VB background and am switching to C # for my new assignment. I am also trying to improve .NET in general. I saw the keyword "T", which is used a lot in people's publications. What does "T" mean in C #? For example:

public class SomeBase<T> where T : SomeBase<T>, new() 

What does T do? Why do I want to use it?

+65
generics c #
Dec 30 '09 at 14:01
source share
7 answers

This is a character for a generic type parameter. It could also be something else, for example:

 public class SomeBase<GenericThingy> where GenericThingy : SomeBase<GenericThingy>, new() 

Only T is used and supported by Microsoft by default.

+40
Dec 30 '09 at 14:05
source share

T is not a per-se keyword, but a placeholder for a type type. See Microsoft Introduction to Generics

The similar syntax for VB.Net would be:

 Public Class SomeBase(Of T As {Class, New})) 
+42
Dec 30 '09 at 14:03
source share

A good example of another name used instead of T would be hash table classes like

 public class Dictionary<K,V> ... 

Where K stands for Key and V for the value. I think T stands for type.

You may have seen this. If you can establish a connection, this should be pretty helpful.

+14
Dec 30 '09 at 14:08
source share

It will be "general." As already mentioned, there is an explanation of Microsoft concept. As for why the โ€œTโ€ - see this question .

In a nutshell, it allows you to create a class / method specialized for a particular type. A classic example is the System.Collections.Generic.List<T> class. It is similar to System.Collections.ArrayList , except that it only stores an element of type T This ensures type safety - you cannot (accidentally or otherwise) put the wrong type in your list. The System.Collections.Generic namespace contains several other types of collections that use this.

As for where you could use it, it's up to you. There are many use cases that occur from time to time. Basically this is a kind of home-made collection (when the built-in is not enough), but it really can be anything.

+5
Dec 30 '09 at 14:12
source share

The best way is to get to know "Generics", many resources on the Internet, here is one

T is not a keyword, but a name, maybe something is valid as far as I know, but T is a convention (when only one type is required, coruse)

+4
Dec 30 '09 at 14:04
source share

This means "any class." It can be "B", "A", whatever. I think T is used because of the "Pattern"

+1
Dec 30 '09 at 14:03
source share

T is the name of the type parameter in the general class. It means Type, but you can also call it Alice.

You use generalizations to enhance reuse in a safe type without unnecessary code duplication. Thus, you do not need to write classes for ListOfIntegers , ListOfStrings , ListOfChars , ListOfPersons , etc., but instead you can write a generic class List<T> , and then create objects of types List<Int32> , List<string> , List<char> and List<Person> . The compiler does the job for you.

+1
Dec 30 '09 at 14:09
source share