title: Alternative to using ref in foreach?
body: <p>I have a modifying method with a signature like</p>
  
  <pre><code>private bool Modify(ref MyClass obj);
  </code></pre>
  
  <p>that will make modifications to <code>obj</code> and indicate succes with it's return value. <code>Modify</code> is not reassigning the reference (I know that this wouldn't work), just modifying instance fields, so I want to use it to do something like the following:</p>
  
  <pre><code>foreach(MyClass obj in myList)
  {
      bool success = Modify(obj);
      // do things depending on success
  }
  </code></pre>
  
  <p>I am running into a problem compiling as <code>obj</code> is "not being passed with the <code>ref</code> keyword". However, if I put the ref keyword in like so:</p>
  
  <pre><code>bool success = Modify(ref obj);
  </code></pre>
  
  <p>I get "cannot use <code>obj</code> as a <code>ref</code>/<code>out</code> because it is a 'foreach iteration variable". I understand that foreach uses an immutable iterator and that's why this doesn't work.</p>
  
  <p>My question is what is the easiest alternative to make something like this work?</p>
  
  <p>I have tried using </p>
  
  <pre><code>foreach(int i = 0; i &lt; myList.Count; i++)
  {
      bool success = Modify(ref myList[i]);
      // do things depending on success
  }
  </code></pre>
  
  <p>but they I get "a property or indexer may not be passed as an out of ref parameter".</p>
  
  <p>Thanks your help.</p>