C# 通用字典中的 TKey 和 TValue 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1098038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:10:11  来源:igfitidea点击:

What is TKey and TValue in a generic dictionary?

c#generics

提问by Blankman

The names TKey and TValue in a dictionary are confusing me. Are they named with that convention for a reason or could they have named it anything?

字典中的 TKey 和 TValue 名称让我感到困惑。他们是否出于某种原因使用该约定命名,或者他们可以将其命名为任何名称?

i.e. if I create a generic, do I have to use some sort of naming convention also?

即如果我创建一个泛型,我是否也必须使用某种命名约定?

回答by Philippe Leybaert

The convention is to prefix type names in generics with a capital T

约定是在泛型中使用大写 T 前缀类型名称

Generics best practices

泛型最佳实践

回答by Meta-Knight

When you create a generic class, you can give any name that you want to a generic type. The convention is to prefix it with "T".

创建泛型类时,可以为泛型类型指定任何名称。约定是在它前面加上“T”。

The pattern that I often see is that when you have only one generic type, it is called "T", but when you have more than one, they have explicit names (like TKey and TValue) to differenciate between the different types.

我经常看到的模式是,当你只有一种泛型类型时,它被称为“T”,但当你有多个时,它们有明确的名称(如 TKey 和 TValue)来区分不同类型。

回答by Mitch Wheat

TKeyis the key's type; TValueis the value 's type.

TKey是键的类型;TValue是值的类型。

eg

例如

Dictionary<string, int>is keyed on a string and returns an int

Dictionary<string, int>以字符串为键并返回一个 int

回答by Marc Gravell

It is conventionto use Tfor generic types (comparable with "templates" in C++ etc).

这是约定要使用T泛型类型(相当于在C ++等“模板”)。

If there is a single type (List<T>) then just Tis fine (there is nothing more to explain); but if there are multiple generic types, the Tprefixes the purpose. Hence TKeyis the generic type of the "key", and TValueof the value. If helps in this case if you know that a dictionary maps keys to values!

如果只有一个类型 ( List<T>) 就T可以了(没有什么要解释的了);但是如果有多个泛型类型,T前缀就是目的。因此TKey是“键”和TValue值的通用类型。如果在这种情况下有帮助,如果您知道字典将键映射到值!

The intellisense will usually tell you what each type-argument means; for example with Func<T1,T2,TResult>:

智能感知通常会告诉您每个类型参数的含义;例如Func<T1,T2,TResult>

  • T1: The type of the first parameter of the method that this delegate encapsulates.
  • T2: The type of the second parameter of the method that this delegate encapsulates.
  • TResult: The type of the return value of the method that this delegate encapsulates.
  • T1: 这个委托封装的方法的第一个参数的类型。
  • T2: 这个委托封装的方法的第二个参数的类型。
  • TResult: 这个委托封装的方法的返回值的类型。

(taken from the type's comment data)

(取自类型的注释数据)

回答by Daniel A. White

They are the types of the generic. The capital T means type.

它们是泛型的类型。大写的 T 表示类型。

List<int> list;

In this example the Tis an int.

在这个例子中T是一个int.

回答by John Saunders

A dictionary is a key/value store. In Dictionary<TKey,TValue>TKey is the type of the Key, and TValue is the Type of the Value.

字典是键/值存储。其中Dictionary<TKey,TValue>TKey为Key的类型,TValue为Value的Type。

It is recommended that you use a similar naming convention if possible in your own generics when there is nore than one type parameter. Otherwise, we get MyGeneric<Q,R,S,T,U,...>

当只有一个类型参数时,建议您在自己的泛型中尽可能使用类似的命名约定。否则,我们得到MyGeneric<Q,R,S,T,U,...>

回答by samjudson

There is a convention to use T to signify the class which your generic class is based on (e.g. List<T>). A dictionary however is based on two classes, so they chose to distinguish them with the Key and Value suffix.

有一个约定使用 T 来表示您的泛型类所基于的类(例如List<T>)。然而,字典基于两个类,因此他们选择使用 Key 和 Value 后缀来区分它们。

They could have chosen anything - elsewhere in other scenarios I have seen T and U used, and other subsequent letters (i.e. V, X etc) if more classes are needed.

他们可以选择任何东西——在其他场景中我见过 T 和 U 使用过,如果需要更多类,还有其他后续字母(即 V、X 等)。