Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法隐式转换列表<;T>;收集<;T>;_C#_Generics_Collections - Fatal编程技术网

C# 无法隐式转换列表<;T>;收集<;T>;

C# 无法隐式转换列表<;T>;收集<;T>;,c#,generics,collections,C#,Generics,Collections,这是一个编译器错误(为了可读性稍微更改) 这件事总是让我困惑。FxCop告诉我们,返回列表是件坏事,从集合派生的类应该优先作为返回类型 此外,FxCop表示可以使用List进行内部数据存储\操作。 好的,我明白了,但我不明白的是编译器抱怨试图隐式地将List转换为Collection。列表的接口是否更收费、更实用? 为什么禁止隐式转换 另一个问题来自上面:新列表(一些集合)构造函数是否昂贵 谢谢, 瓦伦丁·瓦西里耶夫(Valentin Vasiliev)列表并非源于集合——但它确实实现了ICol

这是一个编译器错误(为了可读性稍微更改)

这件事总是让我困惑。FxCop告诉我们,返回列表是件坏事,从
集合
派生的类应该优先作为返回类型

此外,FxCop表示可以使用
List
进行内部数据存储\操作。 好的,我明白了,但我不明白的是编译器抱怨试图隐式地将
List
转换为
Collection
列表
的接口是否更收费、更实用? 为什么禁止隐式转换

另一个问题来自上面:
新列表(一些集合)
构造函数是否昂贵

谢谢,

瓦伦丁·瓦西里耶夫(Valentin Vasiliev)

列表
并非源于
集合
——但它确实实现了
ICollection
。这将是一个更好的返回类型选择

至于
新列表(一些集合)
问题,部分取决于集合是什么。如果它实现了
ICollection
(在执行时),那么构造函数可以使用其
Count
属性创建具有正确初始容量的列表,然后再遍历它并添加每个项。如果它没有实现
ICollection
,那么它只相当于:

List<int> list = new List<int>();
foreach (int x in otherCollection)
{
    list.Add(x);
}
List List=新列表();
foreach(其他集合中的int x)
{
增加(x);
}
在一个方便的构造函数中仍然很好,但是效率不是很高-它不可能,真的

我不相信构造器会对数组做任何狡猾的事情,它可能会这样做——使用
Array.Copy
或其他任何方法一次复制一批,而不是迭代。(同样,如果它是另一个
列表
,它可以从备份数组中获取并直接复制它。)

列表
不会从
集合继承。简单明了。除非
列表
提供了一个操作符来隐式转换到
集合
,否则无法执行此操作。如果可以的话,我建议返回
列表
,因为我相信规则是这样的:

接受尽可能少的压缩接口作为参数。
Return作为一个返回参数,可能是最压缩的类型

这里有一个用C#3.0编写的通用扩展方法,用于将
列表
转换为
集合

使用System.Collections.Generic;
使用System.Collections.ObjectModel;
公共静态类扩展方法
{
公共静态集合到集合(此列表项)
{
集合=新集合();
对于(int i=0;i
它是这样使用的

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>();
列表实体=新列表();
实体。增加(“价值1”);
实体。增加(“价值2”);
实体。增加(“价值3”);
实体。增加(“价值4”);
集合convertedEntities=entities.ToCollection();

为什么不做以下事情:

Collection<string> collection = new Collection<string>(theList);
Collection集合=新集合(列表);

as集合(IList输入)将列表作为构造的一部分。

这是如何从
列表
转换为
集合
(使用LINQ时):

旧功能:

public List<Employee> GetEmployee(int id)
{
     return ( from e in MyDataContext.Employees
                   select new Employee()
                   {
                     e.empId = id
                   }
             ).ToList();
}
using System.Collection.ObjectModel;

public Collection<Employee> GetEmployee(int id)
{
           return new Collection<Employee>( 
               (from e in MyDataContext.Employees
                     select new Employee()
                     {
                       e.empId = id
                     }
                ).ToList() as IList<Employee>
           );
}
public List GetEmployee(int-id)
{
返回(从MyDataContext.Employees中的e)
选择新员工()
{
e、 empId=id
}
).ToList();
}
转换后:

public List<Employee> GetEmployee(int id)
{
     return ( from e in MyDataContext.Employees
                   select new Employee()
                   {
                     e.empId = id
                   }
             ).ToList();
}
using System.Collection.ObjectModel;

public Collection<Employee> GetEmployee(int id)
{
           return new Collection<Employee>( 
               (from e in MyDataContext.Employees
                     select new Employee()
                     {
                       e.empId = id
                     }
                ).ToList() as IList<Employee>
           );
}
使用System.Collection.ObjectModel;
公共集合GetEmployee(内部id)
{
退回新收藏(
(来自MyDataContext.Employees中的e)
选择新员工()
{
e、 empId=id
}
).ToList()作为IList
);
}

您可以使用下面的

public class EmployeeCollection : Collection<Employee>
{
      public EmployeeCollection(IList<Employee> list) : base(list)
      {}

      public EmployeeCollection() : base()
      {}

}

另一方面,没有必要循环。。。您可以只做
.ToList()

ICollection collection=new collection();
使用任何方法填写您的收藏,当您需要列表时,只需执行以下操作:

    List<T> list = collection.ToList();
List List=collection.ToList();
之后,您可以在列表中使用任何您想要的内容

有一个好的编码

为了拯救我的一天,谢谢@Shadi Namrouti
    List<T> list = collection.ToList();