Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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# 我应该使用什么作为方法IEnumerable、IList、Collection或什么的返回类型_C#_.net_Collections_Ienumerable_Ilist - Fatal编程技术网

C# 我应该使用什么作为方法IEnumerable、IList、Collection或什么的返回类型

C# 我应该使用什么作为方法IEnumerable、IList、Collection或什么的返回类型,c#,.net,collections,ienumerable,ilist,C#,.net,Collections,Ienumerable,Ilist,我正在制作一个库,它将被不同的应用程序广泛使用。您可以说它是一种公共库或SDK 目前我正在开发一个函数,该函数获取点列表,对这些点执行一些计算,然后返回更新点列表。所以我这里的问题是我应该使用什么作为返回类型和参数IList,IEnumerable或集合 这里是函数。我不知道用户将如何处理输出。用户如何使用它取决于他。那么,在这里使用什么应该是最好的选择呢 public static IEnumerable<Point2D> PeformSomeCalculation(IEnumer

我正在制作一个库,它将被不同的应用程序广泛使用。您可以说它是一种公共库或SDK

目前我正在开发一个函数,该函数获取点列表,对这些点执行一些计算,然后返回更新点列表。所以我这里的问题是我应该使用什么作为返回类型和参数<代码>IList,
IEnumerable
集合

这里是函数。我不知道用户将如何处理输出。用户如何使用它取决于他。那么,在这里使用什么应该是最好的选择呢

public static IEnumerable<Point2D> PeformSomeCalculation(IEnumerable<Point2D> points)
{
    // Do something,
    return updatedPoints;
}
公共静态IEnumerable peformomecalculation(IEnumerable点)
{
//做点什么,
返回更新点;
}
IList接口继承自
ICollection
IEnumerable
IEnumerable


因此,如果您返回一个
IList
,需要
ICollection
、一个
IEnumerable
或仅仅是一个
IEnumerable
的客户端都会很高兴。

您想操纵点集合吗

如果是这样,请使用或使用
IList
,因为它们会公开
Add
Clear
Remove
方法

如果需要为列表编制索引,则应选择-此接口继承自
ICollection
IEnumerable
,因此这可能是您所追求的


如果不需要修改或索引集合,请使用。

作为参数,您应该使用最具体的所需类型。这意味着,如果您只需要IEnumarable的函数内功能,那么应该使用IEnumarable,而不是ICollection或IList


作为一个返回类型,您应该使用最通用的类型,但是您应该注意ICollection和IList类型。如果在Linq to SQL中使用此函数-此函数具有方法contains,但Linq无法将此函数在此类型中转换为SQL。

通常,我不会尝试对用户将要执行的操作进行假设,除非我尝试强制执行一条规则,例如此集合为只读,因此我将使用最通用的类型。也可以考虑使用<强> <强>,因为您正在接受一个列表并返回一个更新的列表。

使用收益率回报语句可能会方便您的客户使用每种功能。易于实现