views:

982

answers:

4

I realize that you cannot iterate over a Dictionary in C# and edit the underlying Dictionary as in the following example:

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;
}

One way I see to fix this is to make a List backed by the Dictionary's keys, like this:

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;
}

Because I'm not editing the keys themselves, is there any reason that this should not be done or that it's bad to choose this as a solution. (I realize if I was editing those keys, this could cause a lot of problems.)

Thank you.

Edit: Fixed my code example. Sorry about that.

+1  A: 

Your first piece of code looks fine to me - you're not editing the dictionary at all.

David M
+4  A: 

Hi Jason, in your examples it doesn't look to me like you're editing the dictionary values (or keys)?

In general your solution looks fine, you could do it with a bit less code like this:

List<double> total = new List<double>();
foreach (AKeyObject key in aDictionary.Keys.ToList())
{
   for (int i = 0; i < aDictionary[key].Count; i++)
   {
      total[i] += aDictionary[key][i];
   }
}
Alex Black
Unfortunately, I do not have access to "ToList()" - still using Visual Studio 2005 (I'm guessing that is the reason). But, thank you for this suggestion.
JasCav
Your solution looks fine.
Alex Black
The ToList() is completely unnecessary anyway, "in aDictionary.Keys" will work just fine.
Thorarin
@Thorarin: if you leave out the ToList won't you get an exception about modifying the dictionary while enumerating it? (Hence the original question)
Alex Black
+5  A: 

You can loop over dictionaries with using KeyValuePair class.

Dictionary<string, string> d1 = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> val in d1)
{ 
    ...
}
iburlakov
A: 

Here is another more LINQ-esque version:

totalCost = totalCost
     .ToDictionary( kvp => kvp.Key, 5 );

Or, if 5 is not quite what you want :)

totalCost = totalCost
     .ToDictionary( kvp => kvp.Key, CalculateSomething(kvp.Value) );

(Note: this doesn't edit the underlying dictionary, instead it replaces it with a new one)

Alex Black