title: List<T>.ForEach vs. custom IEnumerable<T> extension
body: <p>Say I have a class:</p>
  
  <pre><code>public class MyClass
  {
     ...
  }
  </code></pre>
  
  <p>and a webservice method that returns an <code>IEnumerable&lt;MyClass&gt;</code></p>
  
  <p>The consumer of the webservice defines some method:</p>
  
  <pre><code>public void DoSomething(MyClass myClass)
  {
     ...
  }
  </code></pre>
  
  <p>Now, the consumer can call <code>DoSomething</code> on the result of the webservice method in two ways:</p>
  
  <pre><code>var result = // web service call
  
  foreach(var myClass in result)
  {
     DoSomething(myClass);
  }
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>var result = // web service call
  
  result.ToList().ForEach(DoSomething);
  </code></pre>
  
  <p>Needless to say I much prefer the second way since it is much shorter and more expressive (once you get used to the syntax, which I have).</p>
  
  <p>Now, the web service method only exposes an <code>IEnumerable&lt;MyClass&gt;</code>, but it actually returns a <code>List&lt;MyClass&gt;</code> which (AFAIK) means that the actual serialized object is still a <code>List&lt;T&gt;</code>. However, I have found (using reflector) that the Linq method <code>ToList()</code> makes a copy of all the objects in the <code>IEnumerable&lt;T&gt;</code> regardless of the actual runtime type (in my opinion, it could just have casted the argument to a <code>List&lt;T&gt;</code> if it already was one). </p>
  
  <p>This obviously has some performance overhead, especially for large list (or lists of large objects).</p>
  
  <p>So what can I do to overcome this problem, and why is there no <code>ForEach</code> method in Linq?</p>
  
  <p>By the way, his question is vaguely related to <a href="https://stackoverflow.com/questions/2297100/wcf-operationcontract-which-generic-collection-type-should-i-expose">this one</a>.</p>