Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does enumerating through a collection throw an exception but looping through its items does not
    primarykey
    data
    text
    <p>I was testing out some synchronization constructs and I noticed something that confused me. When I was enumerating through a collection while writing to it at the same time, it threw an exception (this was expected), but when I looped through the collection using a for loop, it did not. Can someone explain this? I thought that a List does not allow a reader and writer to operate at the same time. I would have expected looping through the collection to exhibit the same behavior as using an enumerator.</p> <p><strong>UPDATE: This is a purely academic exercise. I undersand that enumerating a list is bad if it is being written to at the same time. I also understand that I need a synchronization construct. My question again was about why operation one throws an exception as expected but the other does not.</strong></p> <p>Code is below:</p> <pre><code> class Program { private static List&lt;string&gt; _collection = new List&lt;string&gt;(); static void Main(string[] args) { ThreadPool.QueueUserWorkItem(new WaitCallback(AddItems), null); System.Threading.Thread.Sleep(5000); ThreadPool.QueueUserWorkItem(new WaitCallback(DisplayItems), null); Console.ReadLine(); } public static void AddItems(object state_) { for (int i = 1; i &lt;= 50; i++) { _collection.Add(i.ToString()); Console.WriteLine("Adding " + i); System.Threading.Thread.Sleep(150); } } public static void DisplayItems(object state_) { // This will not throw an exception //for (int i = 0; i &lt; _collection.Count; i++) //{ // Console.WriteLine("Reading " + _collection[i]); // System.Threading.Thread.Sleep(150); //} // This will throw an exception List&lt;string&gt;.Enumerator enumerator = _collection.GetEnumerator(); while (enumerator.MoveNext()) { string value = enumerator.Current; System.Threading.Thread.Sleep(150); Console.WriteLine("Reading " + value); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload