I understand that you cannot iterate over a dictionary in C # and edit the basic dictionary, as in the following example:
Dictionary<Resource, double> totalCost = new Dictionary<Resource, double>();
foreach (Resource resource in totalCost.Keys)
{
totalCost[resource] = 5;
}
As I understand it, this is to make a list supported by the dictionary keys, for example:
Dictionary<Resource, double> totalCost = new Dictionary<Resource, double>();
foreach (Resource resource in new List(totalCost.Keys))
{
totalCost[resource] = 5;
}
Since I do not edit the keys themselves, is there a reason why this should not be done, or that it is bad to choose this as a solution. (I understand that if I edited these keys, it could cause a lot of problems.)
Thanks.
Edit: Fixed my sample code. Sorry.
source
share