简体   繁体   English

如何编写通用扩展方法,以使用HashSet.AddRange()将列表转换为HashSet?

[英]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. HashSet没有AddRange方法,因此我想为其编写扩展方法。 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. 我有一个基类Media,一个派生类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() . 但是,编译器告诉我,当我尝试使用AddRange()时,无法将List<Photo>转换为IEnumerable<Media> 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> ? 我很确定这是因为扩展方法中有IEnumerable<T> ,但是如何编写它,使其类型不同于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. 实际上, HashSet<T>可能没有AddRange方法,但是它具有UnionWith方法,这在语义上就是您要寻找的。 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 . 如果您只想将集合复制到HashSet ,它还具有一个将IEnumerable<T>作为参数的构造函数。

In the general case, the above answer recommending to use a generic constraint (" where T : U ") is correct. 在一般情况下,以上答案建议使用一般约束(“ where T : U ”)。

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. 您将需要调用Cast<Media>扩展方法将IEnumerable<Photo>转换为IEnumerable<Media>然后再将其添加到AddRange实现的签名中。

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. 解决方案以在签名中产生正确的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM