C#中的“T”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "T" mean in C#?
提问by Jim
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
?我为什么要使用它?
采纳答案by user39603
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 是 Microsoft 使用和鼓励的默认值。
回答by Otávio Décio
It means "any class". It could be "B", "A", whatever. I think T is used because of "Template"
它的意思是“任何类”。它可以是“B”,“A”,随便什么。我认为使用 T 是因为“模板”
回答by Rob Walker
T is not a keyword per-se but a placeholder for a generic type. See Microsoft's Introduction to Generics
T 本身不是关键字,而是泛型类型的占位符。请参阅 Microsoft 的泛型简介
The equivalent VB.Net syntax would be:
等效的 VB.Net 语法是:
Public Class SomeBase(Of T As {Class, New}))
回答by Yossi Dahan
Best way would be to get yourself familiar with "Generics", many resources on the web, here's one
最好的方法是让自己熟悉“泛型”,网上有很多资源,这是一个
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 是约定(当只需要一种类型时,coruse)
回答by biozinc
A good example of another name used instead of T would
be the hash table classes, e.g.
使用另一个名称代替 T 的一个很好的例子would
是哈希表类,例如
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.
你可能已经看到了这一点。如果您可以建立联系,那应该会很有帮助。
回答by jason
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
,ListOfStrings
,ListOfChars
,ListOfPersons
等等而是可以写一个通用类List<T>
,然后实例的类型的对象List<Int32>
,List<string>
,List<char>
和List<Person>
。编译器为您完成工作。
回答by Vilx-
That would be a "Generic". As people have already mentioned, there is a Microsoft explanationof 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.Generic.List<T>
类。它与 相同System.Collections.ArrayList
,只是它允许您仅存储类型为 的项目T
。这提供了类型安全性 - 您不能(意外或以其他方式)将错误类型的项目放入列表中。该System.Collections.Generic
命名空间包含几个不同的集合类型,其利用了这一点。
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.
至于你可以在哪里使用它 - 这取决于你。有许多用例不时出现。大多数情况下,它是某种自制的集合(当内置集合不够用时),但它真的可以是任何东西。