本文介绍了为什么泛型经常使用T?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在泛型中使用'T'是否有任何理由?它是某种缩写吗?据我所知,一切正常。例如

Is there any reason for the use of 'T' in generics? Is it some kind of abbreviation? As far as I know, everything works. For example

public G Say<G>(){ ... }

甚至是

or even

public Hello Say<Hello>(){ ... }


推荐答案

强>Ť YPE。但这只是一个传统,没有什么可以阻止你使用其他名字。例如,通用字典使用< TKey,TValue>

还有一个如果您有一个单一的类型参数,并且在 T 前加上了描述性参数名称(如果您有多个参数)。这样做可以为您的代码提供更一致的感受。

There is also a Microsoft guideline that recommends using the letter T if you have a single type parameter, and prefix descriptive parameter names with T if you have more than one. Doing so will provide a more consistent feel across your code.

这篇关于为什么泛型经常使用T?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 15:01