title: Best way to test if a Type is a collection
body: <p>I have a method that accepts a parameter <strong>obj</strong> of type System.Object</p>
  
  <p>Now I want to check if the actual type of obj is:</p>
  
  <ul>
  <li>A collection type (IEnumerable).</li>
  <li>Anything else.</li>
  </ul>
  
  <p>The first way I thought of is:</p>
  
  <pre><code>if (obj is IEnumerable) 
     // obj is a collection
  </code></pre>
  
  <p>But System.String implements IEnumerable, and <strong>I don't want to treat string as a collection</strong>.</p>
  
  <p>The second way I thought is testing for ICollection instead of IEnumerable, since IEnumerable is more of a potential collection than an actual one. This would leave out string, but also ICollection-Of-T because it does not inherit ICollection (IEnumerable-Of-T is the only generic collection abstraction that's backwards compatible - it inherits IEnumerable).</p>
  
  <p>So I guess the best way is:</p>
  
  <pre><code>if (obj is string) 
    // not a collection
  else if (obj is IEnumerable) 
    // collection
  else
    // not a collection
  </code></pre>
  
  <p>Is there a better way?</p>