Collection was modified; enumeration operation may not execute.

Dear all,

This is my first article. i have decided to write down on one common error which every developer face at least once and loose couple of minutes or hours from their working hours i.e. Collection was modified; enumeration operation may not execute.

ERROR

Reason:
This error occurred when we try to modify collection i.e. List, SPListCollection(Sharepoint List Collection) etc. which is under enumeration or under iteration.Lets look at following Very simple C# code,

Snippet

class Program
    {
        static void Main(string[] args)
        {
            //Line 1
            List<String> lst = new List<String>();
 
            //Line 2
            lst.Add("a");
 
            //Line 3
            lst.Add("b");
 
            //Line 4
            lst.Add("c");
 
            //Line 5
            foreach (string item in lst)
            {
                //Line 6
                lst.Add("d");
                //This is root cause of above error.
               // we try to update collection which is under enumeration(foreach loop)
              // which is not allowed.
            }
 
 
        }
    }

if you create sample console application and copy paste above code and debug code you will find following things,

1.Until Line No.5 code will run without any error.

2.when control go to Line No.6, problem get started i.e. we try to update collection which is under enumeration operation(foreach loop) which is not allowed.

3.till now there is no error but ground for failure has been already developed. when control again go to the Line No.5, Error get occurred i.e. Collection was modified; enumeration operation may not execute.

4.In Above Code,we try to update (in our case we try to add item) collection i.e. List lst which is under enumeration(foreach loop).

Resolution:-

if you want update collection which under enumeration,please find create new collection and update that newly create collection.

comments and reply are truly appreciated.

Regards,

Vishal Kadam

2 thoughts on “Collection was modified; enumeration operation may not execute.

    1. Hi Santosh,Please check following code as requested,

      class Program
      {
      static void Main(string[] args)
      {
      //Line 1
      List lst = new List();

      //Line 2
      lst.Add(“a”);

      //Line 3
      lst.Add(“b”);

      //Line 4
      lst.Add(“c”);

      //Line 5
      foreach (string item in lst)
      {
      //Line 6
      lst.Add(“d”);
      //This is root cause of above error.
      // we try to update collection which is under enumeration
      // which is not allowed.
      }

      }
      }

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s