开发者

How do I write a generic extension method to convert a List to a HashSet using HashSet.AddRange()?

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. Th开发者_如何转开发is 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>?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜