C# Thread Locking Problem
<p>I am creating a solution which has 2 classes</p>

<p>Class A has a public static List object i.e. <code>public static List&lt;MyClass&gt; classes = new List&lt;MyClass&gt;();</code>
Class B has methods which are running using System.Threading.Timer and call methods of class A on timely intervals</p>

<p>Class A has two methods
Method 1: Update the static List - Finds an item and increment its property e.g.</p>

<pre><code>classes[0].Id = i++;
</code></pre>

<p>Method 2: Return the static List</p>

<p>But I keep getting exception that List cannot be modified.</p>

<p>I want to ensure that only one method is locking the classes object at one time. What can I do to achieve it? lock(this) or lock(classes) didn't work.</p>

<p>Exception is along the lines that </p>

<pre><code>list cannot be modifed when it is being enumerated.
</code></pre>

<p>It points to the line where <code>classes[0].Id</code> is incremented</p>

<p>Code is:</p>

<pre><code>for (int i = 0; i&lt; classes.Count;i++)
{
if (classes[i].Name == this.NameToFind)
{
classes[i].Id = n++;
}

}
</code></pre>

<p><strong>Please note that I want to modify classes list object as the updated object is being returned by the other method.</strong></p>
