我有一个类计数器按键计数的东西。简化:IEnumerable <T, int>,Arity和泛型类型定义
public class Counter<T> {
private Dictionary<T, int> counts;
public void Increment(T key) {
int current;
bool exists = counts.TryGetValue(key, out current);
if (exists) {
counts[key]++;
} else {
counts[key] = 1;
}
}
}
它做了一些专门的,以我的需要其他的东西,但是这是本质。到目前为止,它工作得很好。
现在我想让它在Linq查询中使用(同时使用键和值)。做到这一点,我想我需要实现
IEnumerable<T, int>
于是我说:
public class Counter<T> : IEnumerable<KeyValuePair<T, int>> {
// ...
IEnumerator<KeyValuePair<T, int>>
IEnumerable<KeyValuePair<T, int>>.GetEnumerator()
{
return ((IEnumerable<KeyValuePair<T, int>>)counts).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return counts.GetEnumerator();
}
不幸导致编译器错误
提供的通用参数数量不等同于泛型类型定义的参数。 参数名:实例
问题
- 到底什么是元数?
- 我在正确的道路上使这种类型可用Linq?
- 如何解决实施问题?
UPDATE:错字
我有一个错字,同时简化了我的代码发布。该代码实际上是在试图实施IEnumerable<KeyValuePair<T, int>>
而不是IEnumerable<T, int>
在#1。 IEnumerable。 *一* *这是* arity。*您正试图提供。 –
2013-04-29 03:20:51
@AnthonyPegram:只有一个*泛型类型参数*。其他类型* int *是固定的。这是不允许的?你能帮我一个参考吗? – 2013-04-29 03:23:21
** Downvoter **:我希望知道你在这个问题上找不到什么。 – 2013-04-29 03:24:09