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

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

commented: Thank you :0 +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.