KeyedCollection是什么的集合?

Bor*_*ris 2 c# collections keyedcollection

我想创建一个迭代键控集合的方法.我想确保我的方法支持任何扩展的集合的迭代KeyedCollection<string, Collection<string>>

这是方法:

public void IterateCollection(KeyedCollection<string, Collection<string>> items)
{
    foreach (??? item in items)
    {
        Console.WriteLine("Key: " + item.Key);
        Console.WriteLine("Value: " + item.Value);
    }
}
Run Code Online (Sandbox Code Playgroud)

它显然不起作用,因为我不知道哪种类型应该替换循环中的问号.我不能简单地放objectvar因为我需要稍后在循环体中调用KeyValue属性.我在寻找什么类型的?谢谢.

Jon*_*eet 8

KeyedCollection<TKey, TItem>实现ICollection<TItem>,所以在这种情况下你使用:

foreach(Collection<string> item in items)
Run Code Online (Sandbox Code Playgroud)

这也是var给你的东西.您没有获得键/值对KeyedCollection- 您只需获取值.

是否有可能KeyedCollection真的不适合您使用?