Question

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>?

Was it helpful?

Solution

Yet another generic-variance issue. You will need to call the Cast<Media> extension method to convert the IEnumerable<Photo> to IEnumerable<Media> before it will be acceptable to your AddRange implementation's signature.

Alternatively, you can declare

AddRange<T, U>(ICollection<T>, IEnumerable<U>) where U : T

as a workaround to produce the correct variance in the signature.

OTHER TIPS

Actually, HashSet<T> may not have an AddRange method, but it has a UnionWith method, which is semantically what you are looking for. The difference in name reflects the fact that it filters out duplicates. It also has a constructor that takes an IEnumerable<T> as a parameter, if you just want to copy a collection into a HashSet.

In the general case, the above answer recommending to use a generic constraint ("where T : U") is correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top