title: Cannot assign to item because it is a foreach iteration variable
body: <p>Why can't we assign a value to the local variable in a foreach loop?</p>
  
  <p>I understand this would not update the collection because we are just changing what object the local variable refers to.</p>
  
  <p>If you implement the same functionality using a while loop, it allows you to update the local variable.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          IEnumerable&lt;string&gt; enumerable = new List&lt;string&gt; { "a", "b", "c" };
  
          //This foreach loop does not compile:
          foreach (string item in enumerable)
          {
              item = "d"; //"cannot assign to item because it is a foreach iteration variable"
          }
  
          //The equivalent C# code does compile and allows assinging a value to item:            
          var enumerator = enumerable.GetEnumerator();
          try
          {
              while (enumerator.MoveNext())
              {
                  string item = (string)enumerator.Current;
                  item = "d";
              }
          }
          finally
          {
              enumerator.Dispose();
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  This question is different to the possible duplicate because it's asking why we can't modify the iteration variable when behind the scenes it's just a local variable pointing to the same object as <code>enumerator.Current</code>.</p>