title: How to tell if an IEnumerable<T> is subject to deferred execution?
body: <p>I always assumed that if I was using <code>Select(x=&gt; ...)</code> in the context of LINQ to objects, then the new collection would be immediately created and remain static. I'm not quite sure WHY I assumed this, and its a very bad assumption but I did. I often use <code>.ToList()</code> elsewhere, but often not in this case.</p>
  
  <p>This code demonstrates that even a simple 'Select' is subject to deferred execution :</p>
  
  <pre><code>var random = new Random();
  var animals = new[] { "cat", "dog", "mouse" };
  var randomNumberOfAnimals = animals.Select(x =&gt; Math.Floor(random.NextDouble() * 100) + " " + x + "s");
  
  foreach (var i in randomNumberOfAnimals)
  {
      testContextInstance.WriteLine("There are " + i);
  }
  
  foreach (var i in randomNumberOfAnimals)
  {
      testContextInstance.WriteLine("And now, there are " + i);
  }
  </code></pre>
  
  <p>This outputs the following (the random function is called every time the collection is iterated through): </p>
  
  <pre><code>There are 75 cats
  There are 28 dogs
  There are 62 mouses
  And now, there are 78 cats
  And now, there are 69 dogs
  And now, there are 43 mouses
  </code></pre>
  
  <p>I have many places where I have an <code>IEnumerable&lt;T&gt;</code> as a member of a class. Often the results of a LINQ query are assigned to such an <code>IEnumerable&lt;T&gt;</code>. Normally for me, this does not cause issues, but I have recently found a few places in my code where it poses more than just a performance issue.</p>
  
  <p>In trying to check for places where I had made this mistake I thought I could check to see if a particular <code>IEnumerable&lt;T&gt;</code> was of type <code>IQueryable</code>. This I thought would tell me if the collection was 'deferred' or not. It turns out that the enumerator created by the Select operator above is of type <code>System.Linq.Enumerable+WhereSelectArrayIterator``[System.String,System.String]</code> and not <code>IQueryable</code>.</p>
  
  <p>I used <a href="http://www.red-gate.com/products/reflector/" rel="noreferrer">Reflector</a> to see what this interface inherited from, and it turns out not to inherit from anything that indicates it is 'LINQ' at all - so there is no way to test based upon the collection type.</p>
  
  <p>I'm quite happy now putting <code>.ToArray()</code> everywhere now, but I'd like to have a mechanism to make sure this problem doesn't happen in future. Visual Studio seems to know how to do it because it gives a message about 'expanding the results view will evaluate the collection.'</p>
  
  <p>The best I have come up with is :</p>
  
  <pre><code>bool deferred = !object.ReferenceEquals(randomNumberOfAnimals.First(),
                                          randomNumberOfAnimals.First());
  </code></pre>
  
  <p><strong>Edit:</strong> This only works if a new object is created with 'Select' and it not a generic solution. I'm not recommended it in any case though! It was a little tongue in the cheek of a solution.</p>