0
public void DisplayValue<T>(T field, string fieldname)

What does T stands for in above code?

Whats the benefit to do so?

Where we can use this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MARKAND Bhatt
  • 2,428
  • 10
  • 47
  • 80
  • 1
    So many duplicates... must be close ASAP. – Sachin Feb 14 '14 at 10:56
  • 1
    It really comes from the father of Delphi (Anders Hejlsberg) who was also the lead architect of the team developing the language C#. In Delphi we use T to specify it is a type, for example TClient etc. It just makes sense that Anders applied that in C# generics. I just hope that you are giving the generic type at least some meaningful name and not just T but rather for example TCommand. –  Feb 14 '14 at 11:07

3 Answers3

2

From Generics (C# Programming Guide)

... by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operations

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
1

Type Parameters T: The type of elements in the list.

according to MSDN documentation.

Whats the benefit to do so?

The method can take anonymous lists of a type.

Where we can use this?

For example when the same manipulation has to be done on several lists of several types.

Max
  • 12,622
  • 16
  • 73
  • 101
1

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];
    j=0;
    for(int i=array.Length; 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];
    j=0;
    for(int i=array.Length; 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, 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.

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96