Collection was modified; enumeration operation may not execute.

I create method feedingtime(), all animals eat in order of birds, snake and then monkeys. If an animal does not have enough food then report that it. . . escaped.

public void Feedtime()
{

IEnumerator em = animalList.GetEnumerator();
while(em.MoveNext())
{
if(em.Current.GetType()==typeof(Bird))
{
iFoodPresent -= ((Bird)em.Current).getFoodIntake();
if (iFoodPresent>0)
{
Console.WriteLine(((Bird)em.Current).ToString()+ " have eaten, left food = "+ iFoodPresent);

}
else
{
Console.WriteLine(((Bird)em.Current).ToString()+" escaped");
animalList.Remove((Bird)em.Current);


}
}
}
em = animalList.GetEnumerator();
while(em.MoveNext())
{

if(em.Current.GetType()==typeof(Snake))
{
iFoodPresent -= ((Snake)em.Current).getFoodIntake();
if (iFoodPresent>0)
{
Console.WriteLine(((Snake)em.Current).ToString()+ " have eaten, left food = "+ iFoodPresent);

}
else
{
Console.WriteLine(((Snake)em.Current).ToString()+" escaped");
animalList.Remove((Snake)em.Current);

}
}
}
em = animalList.GetEnumerator();
while(em.MoveNext())
{
if(em.Current.GetType()==typeof(Monkey))
{
iFoodPresent -= ((Monkey)em.Current).getFoodIntake();
if (iFoodPresent>0)
{
Console.WriteLine(((Monkey)em.Current).ToString()+ " have eaten, left food = "+ iFoodPresent);
}
else
{

Console.WriteLine(((Monkey)em.Current).ToString()+" escaped");
animalList.Remove((Monkey)em.Current);


}
}
}
}



Answer this question

Collection was modified; enumeration operation may not execute.

  • spnz

    You cannot alter a collection when using an enumerator or else the enumerator becomes invalid. If you want to remove items from a collection then use a for loop starting at the last index and moving towards the front. That way removing an item does not affect the indices of the items you are yet to visit.
  • Jeremy51

    You can't effect a collection while enumerating. Here is the working code, but you do a very strange thing, you have a loop for every animal type that can be better:


    public void Feedtime()
      {
       for(int i = animalList.Count - 1; i >= 0; i++)
       {
        if(animalList[ i ].GetType()==typeof(Bird))
        {
         iFoodPresent -= ((Bird)animalList[ i ]).getFoodIntake();
         if (iFoodPresent>0)
         {
          Console.WriteLine(((Bird)animalList[ i ]).ToString()+ " have eaten, left food = "+ iFoodPresent);
         
         }
         else
         {
          Console.WriteLine(((Bird)animalList[ i ]).ToString()+" escaped");
          animalList.Remove((Bird)animalList[ i ]);
         
         
         }
        }
       }

       for(int i = animalList.Count - 1; i >= 0; i++)
       {
        if(animalList[ i ].GetType()==typeof(Snake))
        {
         iFoodPresent -= ((Snake)animalList[ i ]).getFoodIntake();
         if (iFoodPresent>0)
         {
          Console.WriteLine(((Snake)animalList[ i ]).ToString()+ " have eaten, left food = "+ iFoodPresent);
        
         }
         else
         {
          Console.WriteLine(((Snake)animalList[ i ]).ToString()+" escaped"); 
          animalList.Remove((Snake)animalList[ i ]);
         

         }
        }
       }
       
       for(int i = animalList.Count - 1; i >= 0; i++)
       {
        if(animalList[ i ].GetType()==typeof(Monkey))
         {
          iFoodPresent -= ((Monkey)animalList[ i ]).getFoodIntake();
          if (iFoodPresent>0)
          {
           Console.WriteLine(((Monkey)animalList[ i ]).ToString()+ " have eaten, left food = "+ iFoodPresent);
          }
          else
          {
           Console.WriteLine(((Monkey)animalList[ i ]).ToString()+" escaped");
           animalList.Remove((Monkey)animalList[ i ]);
          }
         }
        }
       }

     


    A better loop will be (i geus the base type of you animals is Animal):

    public void Feedtime()
    {
        for(int i = animalList.Count - 1; i >= 0; i++)
        {
            Animal animal = (Animal)animalList[ i ];
           
            iFoodPresent -= animal.getFoodIntake();
           
            if( iFoodPresent > 0 )
            {
                Console.WriteLine( animal + " have eaten, left food = " + iFoodPresent );
            }
            else
            {
                animalList.Remove( animal );

                Console.WriteLine( animal + " escaped" );
            }
        }
    }

     



  • Collection was modified; enumeration operation may not execute.