0

I'm using a dictionary to store my Foo classes.

class Foo
    {
        int i = 0;

        public void Update()
        {
            i++;
        }
    }

Dictionary<string, Foo> fooDictionary = new Dictionary<string, Foo>();

The dictionary contains

fooDictionary.Add("MrFoo", new Foo());
            fooDictionary.Add("MrsFoo", new Foo());
            fooDictionary.Add("BabyFoo", new Foo());

How can I iterate through the dictionary without using foreach and call the Update method for all the Foo classes stored?

for (int i = 0; i < fooDictionary.Count; ++i)
            {
                //fooDictionary[i].Update();
            }
2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Teme64
1

You have to use the key as the index for the dictionary. Like this:

for (int i = 0; i < fooDictionary.Count; ++i)
  {
    fooDictionary[fooDictionary.Keys.ElementAt(i)].Update();
  }

HTH

Votes + Comments
Thank you :0
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.