Usually my methods are as the following:
通常我的方法如下:
public List<int> Method1(int input)
{
var output = new List<int>();
//add some items to output
return output;
}
But FxCop advises another IList implementation instead of List, but I can't remember which. The alternatives include returning it as an IList, ICollection or IEnumerable for more flexibility or a whole different way as the code below.:
但是FxCop建议另一个IList实现而不是List,但我不记得哪个。替代方案包括将其作为IList,ICollection或IEnumerable返回以获得更大的灵活性或完全不同的方式,如下面的代码:
public int[] Method2(int input)
{
var output = new List<int>();
//add some items to output
return output.ToArray();
}
Of all alternatives, all provided and all possibilities, which is considered the best practice?
在所有备选方案中,所有提供的和所有可能性,被认为是最佳做法?
8 个解决方案
#1
5
"Framework Design Guidelines" (2nd ed) in §8.3.1 has quite a lot to say about collections as return values, summary:
§8.3.1中的“框架设计指南”(第2版)对于作为返回值的集合有很多说法,摘要:
- DO NOT provide settable collection properties.
- 不要提供可设置的集合属性。
-
DO use
Collection<T>
or a subclass ofCollection<T>
for properties or return values representing read/write collections. -
请使用Collection
或Collection 的子类来表示属性或返回表示读/写集合的值。 -
DO use
ReadOnlyCollection<T>
, a subclass ofReadOnlyCollection<T>
, or in rare casesIEnumerable<T>
for properties or return values representing read-only collections. -
请使用ReadOnlyCollection
,ReadOnlyCollection 的子类,或者在极少数情况下使用IEnumerable 表示属性或返回表示只读集合的值。
(and more, but these three capture the core).
(以及更多,但这三个捕获核心)。
The first of those above: don't return reference to internal collection unless you want the user to be able to change it (and then likely you should have a custom type so you have a degree of control).
上面的第一个:不要返回对内部集合的引用,除非您希望用户能够更改它(然后您可能应该有一个自定义类型,以便您有一定程度的控制)。
I would return IList<T>
and ensure I do not define the actual type I am returning, unless I was returning an iterator (when I would use IEnumerable<T>
).
我会返回IList
#2
6
IEnumerable / IEnumerable <T> unless you specifically need a list, then you should return IList <T>
IEnumerable / IEnumerable
#3
5
Eric Lippert has a good post on why returning an array is usually a bad idea.
Eric Lippert有一篇很好的文章说明为什么返回数组通常是一个坏主意。
Typically, you should be as general as you can be without causing undue grief to whoever is going to be calling your method. Prefer interfaces over concrete classes, and pick the most generic interface you can get away with.
通常情况下,你应该尽可能地保持一般性,而不会给任何想要调用你方法的人带来过度的悲伤。首选接口而不是具体类,并选择最常用的接口。
Returning interfaces better encapsulates your implementation, and will make things easier to change in the future. If you start with a concrete type, you're committed to always returning that type.
返回接口可以更好地封装您的实现,并且将来更容易更改。如果从具体类型开始,则承诺始终返回该类型。
IEnumerable<T>
is the best place to start. With the advent of LINQ, there are very few things a caller can't do easily just given an enumeration. If a caller occasionally needs a list, it's easy enough to call .ToList()
.
IEnumerable
If callers are likely to have a specific need to index into the returned collection, or if they are likely going to want to modify the collection themselves (inserting/removing/reordering items), consider using an IList<T>
.
如果调用者可能特定需要索引返回的集合,或者他们可能想要自己修改集合(插入/删除/重新排序项目),请考虑使用IList
#4
3
ReadOnlyCollection<T>
is another option.
ReadOnlyCollection
#5
2
Return the interface that you need in the code calling this method.
在调用此方法的代码中返回所需的接口。
If you need to do list actions on the result, return IList<T>
. If you just need to enumerate the results, return IEnumerable<T>
.
如果需要对结果执行列表操作,请返回IList
Those are the ones I use most, actually. I never return arrays from public interfaces unless there's a very good reason for it.
实际上,这些是我最常用的。除非有很好的理由,否则我永远不会从公共接口返回数组。
#6
1
It depends.
这取决于。
Do you want the caller to be able to modify the items and for you to see those changes? Arrays can be modified. The IList interface defines methods for modification (but the implementation may not allow it).
您是否希望调用者能够修改项目并让您查看这些更改?可以修改数组。 IList接口定义了修改方法(但实现可能不允许)。
Can you elaborate on the FxCop warning?
你能详细说明FxCop警告吗?
Kent
肯特
#7
1
It depends on your requirement. All things said and done, iteration through strongly typed arrays is the fastest amongst all collection types. If you do not need to resize/add/search through them, arrays are quite fine.
这取决于您的要求。所有事情都说完了,通过强类型数组的迭代是所有集合类型中最快的。如果您不需要调整大小/添加/搜索它们,阵列就可以了。
#8
1
I almost always go with List
because it gives me methods that I frequently find the most useful.
我几乎总是选择List,因为它给了我经常发现最有用的方法。
One potential negative consequence of returning an IEnumerable
is that any exceptions that get thrown upon enumeration would come from an area of code that could be quite far away from the code that actually built the object, making bug tracking harder.
返回IEnumerable的一个潜在负面结果是,枚举时抛出的任何异常都来自代码区域,这些代码区域可能与实际构建对象的代码相距甚远,从而使错误跟踪变得更加困难。
#1
5
"Framework Design Guidelines" (2nd ed) in §8.3.1 has quite a lot to say about collections as return values, summary:
§8.3.1中的“框架设计指南”(第2版)对于作为返回值的集合有很多说法,摘要:
- DO NOT provide settable collection properties.
- 不要提供可设置的集合属性。
-
DO use
Collection<T>
or a subclass ofCollection<T>
for properties or return values representing read/write collections. -
请使用Collection
或Collection 的子类来表示属性或返回表示读/写集合的值。 -
DO use
ReadOnlyCollection<T>
, a subclass ofReadOnlyCollection<T>
, or in rare casesIEnumerable<T>
for properties or return values representing read-only collections. -
请使用ReadOnlyCollection
,ReadOnlyCollection 的子类,或者在极少数情况下使用IEnumerable 表示属性或返回表示只读集合的值。
(and more, but these three capture the core).
(以及更多,但这三个捕获核心)。
The first of those above: don't return reference to internal collection unless you want the user to be able to change it (and then likely you should have a custom type so you have a degree of control).
上面的第一个:不要返回对内部集合的引用,除非您希望用户能够更改它(然后您可能应该有一个自定义类型,以便您有一定程度的控制)。
I would return IList<T>
and ensure I do not define the actual type I am returning, unless I was returning an iterator (when I would use IEnumerable<T>
).
我会返回IList
#2
6
IEnumerable / IEnumerable <T> unless you specifically need a list, then you should return IList <T>
IEnumerable / IEnumerable
#3
5
Eric Lippert has a good post on why returning an array is usually a bad idea.
Eric Lippert有一篇很好的文章说明为什么返回数组通常是一个坏主意。
Typically, you should be as general as you can be without causing undue grief to whoever is going to be calling your method. Prefer interfaces over concrete classes, and pick the most generic interface you can get away with.
通常情况下,你应该尽可能地保持一般性,而不会给任何想要调用你方法的人带来过度的悲伤。首选接口而不是具体类,并选择最常用的接口。
Returning interfaces better encapsulates your implementation, and will make things easier to change in the future. If you start with a concrete type, you're committed to always returning that type.
返回接口可以更好地封装您的实现,并且将来更容易更改。如果从具体类型开始,则承诺始终返回该类型。
IEnumerable<T>
is the best place to start. With the advent of LINQ, there are very few things a caller can't do easily just given an enumeration. If a caller occasionally needs a list, it's easy enough to call .ToList()
.
IEnumerable
If callers are likely to have a specific need to index into the returned collection, or if they are likely going to want to modify the collection themselves (inserting/removing/reordering items), consider using an IList<T>
.
如果调用者可能特定需要索引返回的集合,或者他们可能想要自己修改集合(插入/删除/重新排序项目),请考虑使用IList
#4
3
ReadOnlyCollection<T>
is another option.
ReadOnlyCollection
#5
2
Return the interface that you need in the code calling this method.
在调用此方法的代码中返回所需的接口。
If you need to do list actions on the result, return IList<T>
. If you just need to enumerate the results, return IEnumerable<T>
.
如果需要对结果执行列表操作,请返回IList
Those are the ones I use most, actually. I never return arrays from public interfaces unless there's a very good reason for it.
实际上,这些是我最常用的。除非有很好的理由,否则我永远不会从公共接口返回数组。
#6
1
It depends.
这取决于。
Do you want the caller to be able to modify the items and for you to see those changes? Arrays can be modified. The IList interface defines methods for modification (but the implementation may not allow it).
您是否希望调用者能够修改项目并让您查看这些更改?可以修改数组。 IList接口定义了修改方法(但实现可能不允许)。
Can you elaborate on the FxCop warning?
你能详细说明FxCop警告吗?
Kent
肯特
#7
1
It depends on your requirement. All things said and done, iteration through strongly typed arrays is the fastest amongst all collection types. If you do not need to resize/add/search through them, arrays are quite fine.
这取决于您的要求。所有事情都说完了,通过强类型数组的迭代是所有集合类型中最快的。如果您不需要调整大小/添加/搜索它们,阵列就可以了。
#8
1
I almost always go with List
because it gives me methods that I frequently find the most useful.
我几乎总是选择List,因为它给了我经常发现最有用的方法。
One potential negative consequence of returning an IEnumerable
is that any exceptions that get thrown upon enumeration would come from an area of code that could be quite far away from the code that actually built the object, making bug tracking harder.
返回IEnumerable的一个潜在负面结果是,枚举时抛出的任何异常都来自代码区域,这些代码区域可能与实际构建对象的代码相距甚远,从而使错误跟踪变得更加困难。