title: Modifying collection inside foreach loop using IEnumerable Where()
body: <p>Although the iterator variable in a <code>foreach</code> loop is immutable, I am able to modify my collection in the example below if I use <code>Where()</code> to do the modification:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          var list = new List&lt;int&gt; { 1, 2, 3, 4 };
  
          foreach (var listElement in list)
          {
              Console.WriteLine(listElement + " ");
              list = list.Where(x =&gt; x != listElement).ToList();
          }
  
          Console.WriteLine("Count: " + list.Count);
      }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1
  2
  3
  4
  Count: 0
  </code></pre>
  
  <p>Could someone please explain this behavior?</p>