I have a VB background and I'm converting to C# for my new job. I'm also trying to get better at .NET in general. I've seen the keyword "T" used a lot in samples people post. What does the "T" mean in C#? For example:
我有一个VB背景,我正在转换到c#的新工作。我也在努力提高。net的整体水平。我曾见过关键词“T”在人们发布的样本中使用了很多。在c#中“T”是什么意思?例如:
public class SomeBase<T> where T : SomeBase<T>, new()
What does T
do? Why would I want to use it?
T是干什么的?我为什么要用它?
7 个解决方案
#1
41
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.
只有T是微软使用和鼓励的默认设置。
#2
43
T is not a keyword per-se but a placeholder for a generic type. See Microsoft's Introduction to Generics
T本身不是关键字,而是泛型类型的占位符。参见微软的泛型介绍
The equivalent VB.Net syntax would be:
VB。净的语法是:
Public Class SomeBase(Of T As {Class, New}))
#3
14
A good example of another name used instead of T would
be the hash table classes, e.g.
用另一个名字代替T的一个很好的例子就是哈希表类。
public class Dictionary<K,V> ...
Where K
stands for Key and V
for value. I think T
stands for type.
K代表Key V代表value。我认为T代表类型。
You might have seen this around. If you can make the connection, it should be fairly helpful.
你可能见过这个。如果你能建立联系,这应该会很有帮助。
#4
5
That would be a "Generic". As people have already mentioned, there is a Microsoft explanation of the concept. As for why the "T" - see this question.
这将是一种“通用”。正如人们已经提到的,微软对这个概念有一个解释。至于为什么是“T”,请看这个问题。
In a nutshell, it allows you to create a class/method which is specialized to a specific type. A classical example is the System.Collections.Generic.List<T>
class. It's the same as System.Collections.ArrayList
, except that it allows you to store only item of type T
. This provides type safety - you can't (accidentally or otherwise) put items of the wrong type in your list. The System.Collections.Generic
namespace contains several other different collection types which make use of this.
简单地说,它允许您创建专门针对特定类型的类/方法。一个典型的例子是system . collections.general。< T >类列表。它和System.Collections是一样的。ArrayList,除了它只允许存储类型为t的项之外,它提供了类型安全性——您不能(偶然地或其他地)将类型为错误的项放入列表中。System.Collections。泛型名称空间包含其他几种不同的集合类型,它们利用了这一点。
As for where you could use it - that's up to you. There are many use-cases which come up from time to time. Mostly it's some kind of a self-made collection (when the builtin ones don't suffice), but it could really be anything.
至于你可以在哪里使用它——这取决于你。有许多用例不时出现。大多数情况下,它是一种自制的收藏(当内置的不够时),但它可以是任何东西。
#5
4
Best way would be to get yourself familiar with "Generics", many resources on the web, here's one
最好的方法是熟悉“泛型”,web上的许多资源,这里有一个
T is not a keyword but a name, could be anything really as far as I know, but T is the convention (when only one type is needed, of coruse)
T不是关键字而是名字,就我所知,它可以是任何东西,但T是惯例(当只需要一种类型时,是相互关联的)
#6
1
It means "any class". It could be "B", "A", whatever. I think T is used because of "Template"
它的意思是“阶级”。它可以是B, A,等等。我认为T是用在模板上的
#7
1
The T
is the name for the type parameter in a generic class. It stands for "Type" but you could just as well call it "Alice."
T是泛型类中的类型参数的名称。它代表“类型”,但你也可以把它叫做“爱丽丝”。
You use generics to increase reusability in a type-safe manner without needlessly duplicating code. Thus, you do not need to write classes for ListOfIntegers
, ListOfStrings
, ListOfChars
, ListOfPersons
and so on but can instead write a generic class List<T>
and then instantiate objects of types List<Int32>
, List<string>
, List<char>
and List<Person>
. The compiler does the work for you.
您可以使用泛型以一种类型安全的方式增加可重用性,而不必重复代码。因此,您不需要为ListOfIntegers、listofstring、ListOfChars、ListOfPersons等编写类,而是可以编写一个泛型类列表
#1
41
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.
只有T是微软使用和鼓励的默认设置。
#2
43
T is not a keyword per-se but a placeholder for a generic type. See Microsoft's Introduction to Generics
T本身不是关键字,而是泛型类型的占位符。参见微软的泛型介绍
The equivalent VB.Net syntax would be:
VB。净的语法是:
Public Class SomeBase(Of T As {Class, New}))
#3
14
A good example of another name used instead of T would
be the hash table classes, e.g.
用另一个名字代替T的一个很好的例子就是哈希表类。
public class Dictionary<K,V> ...
Where K
stands for Key and V
for value. I think T
stands for type.
K代表Key V代表value。我认为T代表类型。
You might have seen this around. If you can make the connection, it should be fairly helpful.
你可能见过这个。如果你能建立联系,这应该会很有帮助。
#4
5
That would be a "Generic". As people have already mentioned, there is a Microsoft explanation of the concept. As for why the "T" - see this question.
这将是一种“通用”。正如人们已经提到的,微软对这个概念有一个解释。至于为什么是“T”,请看这个问题。
In a nutshell, it allows you to create a class/method which is specialized to a specific type. A classical example is the System.Collections.Generic.List<T>
class. It's the same as System.Collections.ArrayList
, except that it allows you to store only item of type T
. This provides type safety - you can't (accidentally or otherwise) put items of the wrong type in your list. The System.Collections.Generic
namespace contains several other different collection types which make use of this.
简单地说,它允许您创建专门针对特定类型的类/方法。一个典型的例子是system . collections.general。< T >类列表。它和System.Collections是一样的。ArrayList,除了它只允许存储类型为t的项之外,它提供了类型安全性——您不能(偶然地或其他地)将类型为错误的项放入列表中。System.Collections。泛型名称空间包含其他几种不同的集合类型,它们利用了这一点。
As for where you could use it - that's up to you. There are many use-cases which come up from time to time. Mostly it's some kind of a self-made collection (when the builtin ones don't suffice), but it could really be anything.
至于你可以在哪里使用它——这取决于你。有许多用例不时出现。大多数情况下,它是一种自制的收藏(当内置的不够时),但它可以是任何东西。
#5
4
Best way would be to get yourself familiar with "Generics", many resources on the web, here's one
最好的方法是熟悉“泛型”,web上的许多资源,这里有一个
T is not a keyword but a name, could be anything really as far as I know, but T is the convention (when only one type is needed, of coruse)
T不是关键字而是名字,就我所知,它可以是任何东西,但T是惯例(当只需要一种类型时,是相互关联的)
#6
1
It means "any class". It could be "B", "A", whatever. I think T is used because of "Template"
它的意思是“阶级”。它可以是B, A,等等。我认为T是用在模板上的
#7
1
The T
is the name for the type parameter in a generic class. It stands for "Type" but you could just as well call it "Alice."
T是泛型类中的类型参数的名称。它代表“类型”,但你也可以把它叫做“爱丽丝”。
You use generics to increase reusability in a type-safe manner without needlessly duplicating code. Thus, you do not need to write classes for ListOfIntegers
, ListOfStrings
, ListOfChars
, ListOfPersons
and so on but can instead write a generic class List<T>
and then instantiate objects of types List<Int32>
, List<string>
, List<char>
and List<Person>
. The compiler does the work for you.
您可以使用泛型以一种类型安全的方式增加可重用性,而不必重复代码。因此,您不需要为ListOfIntegers、listofstring、ListOfChars、ListOfPersons等编写类,而是可以编写一个泛型类列表