title: How do I write a generic extension method to convert a List to a HashSet using HashSet.AddRange()?
body: <p>HashSet does not have an AddRange method, so I want to write an extension method for it. This is what I have:</p>
  
  <pre><code>public static void AddRange&lt;T&gt;(this ICollection&lt;T&gt; collection, IEnumerable&lt;T&gt; list)
  {
      foreach (var item in list)
      {
          collection.Add(item);
      }
  }
  </code></pre>
  
  <p>I have a base class, Media, and a derived class, Photo. This is the code that I want to work:</p>
  
  <pre><code>var photos = new List&lt;Photo&gt;();
  var media = new HashSet&lt;Media&gt;();
  media.AddRange(photos);
  </code></pre>
  
  <p>However, the compiler is telling me that it can't convert the <code>List&lt;Photo&gt;</code> to <code>IEnumerable&lt;Media&gt;</code> when I try to use <code>AddRange()</code>. I'm pretty sure this is because I have <code>IEnumerable&lt;T&gt;</code> in the extension method, but how do I write it so that the type is different than the type in <code>AddRange&lt;T&gt;</code>?</p>