无法将List <T>隐式转换为Collection <T>

Val*_*yev 42 c# generics collections

这是编译器错误(为了便于阅读,略有改动).

这个总是困惑我.FxCop告诉我返回List是一件坏事,而派生的Collection<T>派对应该是优先的返回类型.

此外,FxCop表示可以List<T>用于内部数据存储\操作.好吧,我明白了,但我没有得到的是编译器抱怨尝试隐式转换List<T>Collection<T>.没有List<T>更多的接口充电和功能?为什么要禁止隐式转换?

另一个源于上述问题:new List<int>(some collection<int>)构造函数昂贵吗?

谢谢,

瓦伦丁瓦西里耶夫

Mat*_* M. 94

为什么不这样做:

Collection<string> collection = new Collection<string>(theList);
Run Code Online (Sandbox Code Playgroud)

as Collection(IList输入)将List作为构造的一部分.


Jon*_*eet 37

List<T>不是来自Collection<T>- 它确实实现了ICollection<T>.这将是返回类型的更好选择.

至于new List<int>(some collection<int>)问题 - 它部分取决于收集的内容.如果它实现ICollection<T>(在执行时),那么构造函数可以使用其Count属性创建具有正确初始容量的列表,然后迭代它并添加每个项目.如果它没有实现ICollection<T>那么它只相当于:

List<int> list = new List<int>();
foreach (int x in otherCollection)
{
    list.Add(x);
}
Run Code Online (Sandbox Code Playgroud)

在一个方便的构造函数中仍然很好,但效率不高 - 它不可能,真的.

我不相信构造函数会对数组做任何狡猾的事情,它可能会使用Array.Copy或者只是一次性复制批次而不是迭代.(同样,如果它是另一个,List<T>它可以进入后备阵列并直接复制.)


Dav*_*ton 6

List<T>不继承自Collection<T>.干净利落.除非List<T>提供操作符隐式转换为/从Collection<T>,否则您无法执行此操作.我实际上会建议List<T>你回来,因为我相信这些规则是这样的:

作为参数接受可能的最不紧缩的接口.作为返回参数返回可能的最紧缩类型.


小智 5

下面是一个用 C# 3.0 编写的通用扩展方法,用于转换List<T>Collection<T>

using System.Collections.Generic;
using System.Collections.ObjectModel;

public static class ExtensionMethods
{
    public static Collection<T> ToCollection<T>(this List<T> items)
    {
        Collection<T> collection = new Collection<T>();

        for (int i = 0; i < items.Count; i++)
        {
            collection.Add(items[i]);
        }

        return collection;
    }
}
Run Code Online (Sandbox Code Playgroud)

它是这样使用的......

List<string> entities = new List<string>();
entities.Add("Value 1");
entities.Add("Value 2");
entities.Add("Value 3");
entities.Add("Value 4");

Collection<string> convertedEntities = entities.ToCollection<string>();
Run Code Online (Sandbox Code Playgroud)