What Does "T" Mean in C#

What does T denote in C#

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.

What does T mean in C#?

It's a symbol for a generic type parameter. It could just as well be something else, for example:

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

Only T is the default one used and encouraged by Microsoft.

In c# what does 'where T : class' mean?

Simply put this is constraining the generic parameter to a class (or more specifically a reference type which could be a class, interface, delegate, or array type).

See this MSDN article for further details.

What does T @this mean in a delegate declaration?

The @this means you can use the keyword this as a variable.

The T is simply the first open generic type of WeakEventHandler<T, E>.

What does T? mean in Class declaration?

No, you've misread. There is no class MyClass<T?>.

There's class MyClass<T> : OtherClass<T?> where T : struct, which is perfectly legal.

In other words, you can do this:

class NullableList<T> : List<T?> where T : struct {}

And you'll get a generic list, which uses a nullable type as its generic type, eg. if you create a NullableList<int>, it will have a method Add(int? val).

Funnily enough, through the syntactic sugar magic of nullables, you can't have a Nullable of a Nullable (eg. Nullable<Nullable<int>> for example), even though Nullable<T> is a struct. It produces a compile-time error.

What does this mean: ClassName T where T : class

We are restricting it to consumable via a class as generic parameter and not to be used with struct (Value Types).

if we don't apply the constraint of the class on T, it would be usable with struct too and here the author has limited this generic class to usable on with T parameter with a reference type i.e. class as type parameter.

For more details, please refer to the MSDN docs on Type Constraint.

What does where T : class, new() mean?

That is a constraint on the generic parameter T. It must be a class (reference type) and must have a public parameter-less default constructor.

That means T can't be an int, float, double, DateTime or any other struct (value type).

It could be a string, or any other custom reference type, as long as it has a default or parameter-less constructor.

What does T : new() mean with generics?

new() mean that T must have default(parameterless) ctor.

Constraints on Type Parameters (C# Programming Guide)



Related Topics



Leave a reply



Submit