title: Checking if a list is empty with LINQ
body: <p>What's the "best" (taking both speed and readability into account) way to determine if a list is empty? Even if the list is of type <code>IEnumerable&lt;T&gt;</code> and doesn't have a Count property.</p>
  
  <p>Right now I'm tossing up between this:</p>
  
  <pre><code>if (myList.Count() == 0) { ... }
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>if (!myList.Any()) { ... }
  </code></pre>
  
  <p>My guess is that the second option is faster, since it'll come back with a result as soon as it sees the first item, whereas the second option (for an IEnumerable) will need to visit every item to return the count.</p>
  
  <p>That being said, does the second option look as readable to you? Which would you prefer? Or can you think of a better way to test for an empty list?</p>
  
  <p><strong>Edit</strong> @lassevk's response seems to be the most logical, coupled with a bit of runtime checking to use a cached count if possible, like this:</p>
  
  <pre><code>public static bool IsEmpty&lt;T&gt;(this IEnumerable&lt;T&gt; list)
  {
      if (list is ICollection&lt;T&gt;) return ((ICollection&lt;T&gt;)list).Count == 0;
  
      return !list.Any();
  }
  </code></pre>