gpt4 book ai didi

c# - KeyedCollection 是什么的集合?

转载 作者:行者123 更新时间:2023-11-30 19:19:47 26 4
gpt4 key购买 nike

我想创建一个方法来遍历键控集合。我想确保我的方法支持迭代任何扩展 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);
}
}

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

最佳答案

KeyedCollection<TKey, TItem>工具 ICollection<TItem> , 所以在这种情况下你会使用:

foreach(Collection<string> item in items)

这就是var也会给你。你不会在 KeyedCollection得到键/值对- 你只是得到了值(value)。

有没有可能 KeyedCollection真的不是最适合您使用的类型吗?

关于c# - KeyedCollection 是什么的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8095154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com