我知道在C#中无法迭代字典并像以下示例那样编辑基础字典:
Dictionary<Resource, double> totalCost = new Dictionary<Resource, double>();
// Populate the Dictionary in here - (not showing code).
foreach (Resource resource in totalCost.Keys)
{
totalCost[resource] = 5;
}
我认为解决这个问题的一种方法是创建一个由字典键支持的列表,如下所示:
Dictionary<Resource, double> totalCost = new Dictionary<Resource, double>();
// Populate the Dictionary in here - (not showing code).
foreach (Resource resource in new List(totalCost.Keys))
{
totalCost[resource] = 5;
}
因为我不是在编辑密钥本身,所以选择这种解决方案是否有任何不良影响或者是不好的呢?(如果我编辑这些密钥,可能会引起很多问题。)
谢谢。
编辑:已修复我的代码示例。对此感到抱歉。