问题描述
有点基本的问题,但是似乎让我很困惑.
A bit of a basic question, but one that seems to stump me, nonetheless.
给出嵌套通用":
IEnumerable<KeyValuePair<TKey, TValue>>
这是否表明IEnumerable可以具有本身就是KeyValuePair的泛型类型?
Is this stating that IEnumerable can have generic types that are themselves KeyValuePair 's ?
谢谢
斯科特
推荐答案
是.KeyValuePair类型需要两个泛型类型参数.我们可以通过指向具体类型来填充它们:
Yes. The KeyValuePair type expects two generic type parameters. We can either populate them by pointing to concrete types:
IEnumerable<KeyValuePair<string, int>>
或者我们可以使用外部类已经指定的其他通用参数来填充它们:
Or we can populate them by using other generic parameters already specified by the outer class:
class Dictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
泛型类型参数总是在使用时指定的,或者在您使用需要它们的类或方法时指定.就像其他任何参数一样,您可以用一个恒定的硬编码值(在这种情况下为类型)或另一个变量来填充它.
Generic type parameters are always specified "at-use", or at the point where you are using the class or method that requires them. And just like any other parameter, you can fill it with a constant, hard-coded value (or type in this case), or another variable.
这篇关于C#中的嵌套泛型是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!