T在C#中代表什么? [英] What does T stands for in C#?

查看:116
本文介绍了T在C#中代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在上面的代码中,T代表什么?



这样做的好处是什么?

我们在哪里可以使用这个?

解决方案

这是一个通用类型参数。



泛型类型参数允许您在编译时为方法指定任意类型的T,而无需在方法或类声明中指定具体类型。



例如:

  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 ++;
}
返回结果;
}

颠倒数组中的元素。这里的关键是数组元素可以是任何类型,并且该函数仍然可以工作。您在方法调用中指定类型;型式安全仍然有保证。

因此,要反转字符串数组:

  string [ ] array = new string [] {1,2,3,4,5}; 
var result = reverse(array);将生成一个字符串数组,结果为{5,4,3,2,1}

这与您调用普通(非泛型)方法的效果相同,如下所示:

  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 ++;
}
返回结果;

}编译器认为该数组包含字符串,因此它返回一个字符串数组。类型字符串被替换为T类型参数。






泛型类型参数也可用于创建泛型类。 p>

在你给SampleCollection的例子中,T是任意类型的占位符;这意味着SampleCollection可以表示一个对象集合,它是您创建集合时指定的对象集合。



所以:

var collection = new SampleCollection< string>();



创建一个集合,字符串。上面所示的Reverse方法,以一种稍微不同的形式,可以用来反转集合的成员。


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?

解决方案

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.

这篇关于T在C#中代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆