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