Possible Duplicate:
C# - List<T> or IList<T>可能重复:C# - List
或IList
It's written all over SO that you should return IList<T>
from your methods and not List<T>
but I can't find any really good reasons why. I keep finding code that does this, and then the calling code usually does one of two things:
你写的应该从你的方法返回IList
- Call
new List<T>(returnedIList)
so it can use all the nice methods on List - Casts back to
List<T>
so it can use all the nice methods on List
调用new List
转换回List
The first one is clunky and the second one would throw (runtime) InvalidCastException
if the implementation actually changed to something else anyway (which makes it totally stupid).
第一个是笨重的,第二个是抛出(运行时)InvalidCastException,如果实现实际上已经改变为其他东西(这使得它完全是愚蠢的)。
If I use List<T>
and for some reason have to replace it with an implementation of IList<T>
that I can't inherit from List<T>
then I'll get build errors and have to change some code. That's probably very unlikely and if it happens, it's not a lot of work to fix. Surely it's not worth losing the benefits of List<T>
and/or having to cast/new List<T>
(Exists, Find, etc.) to get them back for this unlikely scenario?
如果我使用List
So, are there other reasons are there for returning IList<T>
?
那么,返回IList
4 个解决方案
#1
1
The reason is so that your method can be used with anything that implements IList<T>
, and not just a List. It gets even worse, though, since the advent of Linq, I've started making a lot of stuff return Enumerable<T>
or even just IEnumerable
!
原因是你的方法可以用于实现IList
I am not sure I understand the difficulty, though. If something is returning an actual list, and its return depends on that, or its use is specific to that, then it should return List<T>
. If not, then you should have no need to cast it to a List.
不过,我不确定我是否理解这个难点。如果某些东西返回一个实际的列表,并且它的返回依赖于它,或者它的使用是特定的,那么它应该返回List
#2
10
It sounds to me like you're looking at some poor quality code.
听起来像你在看一些质量差的代码。
Returning IList<T>
rather than List<T>
allows your code to be more flexible. You can replace the implementation with any collection that implements IList<T>
without breaking any calling code. That's a good thing...but only when the functionality defined in IList<T>
matches your needs.
返回IList
You can generalize that to say that you should always return the most generic type possible. In most cases, you can get away with IEnumerable<T>
but if you need more functionality, then IList<T>
works. If that doesn't cut it, return the concrete type and be done with it.
您可以概括地说,您应该始终返回最通用的类型。在大多数情况下,您可以使用IEnumerable
In both of the situations you mention, there is a need to use something not directly provided by IList<T>
(and if there's not, then both of those methods are in error). In those cases, either the method should return List<T>
to provide the functionality needed or the caller should be using another method.
在你提到的两种情况中,都需要使用IList
#3
1
Really you should return IEnumerable if possible, otherwise IList.
真的,如果可能你应该返回IEnumerable,否则IList。
Th reason is that you may want to use something else than a List in the future. It is not uncommon to implement your own list that implements IList and then you do not need to change most of your code.
原因是您可能希望将来使用除List之外的其他内容。实现自己的实现IList的列表并不常见,您不需要更改大部分代码。
Search for derived types from IList and you see that you get many hits just within the .NET framework!
从IList中搜索派生类型,您会发现在.NET框架内获得了很多命中!
#4
1
The idea is to let the caller decide what collection they'll use. You see a lot of people returning IEnumerable<T>
whenever they can. It's generally considered good practice to do so. Returning IList let's the caller use whichever implementation of IList they prefer when returning List requires them to manually copy the List's data in their data collection of choice.
我们的想法是让来电者决定他们将使用哪些收藏品。你会看到很多人随时都会返回IEnumerable
Returning IEnumerable is ideal.
返回IEnumerable是理想的。
#1
1
The reason is so that your method can be used with anything that implements IList<T>
, and not just a List. It gets even worse, though, since the advent of Linq, I've started making a lot of stuff return Enumerable<T>
or even just IEnumerable
!
原因是你的方法可以用于实现IList
I am not sure I understand the difficulty, though. If something is returning an actual list, and its return depends on that, or its use is specific to that, then it should return List<T>
. If not, then you should have no need to cast it to a List.
不过,我不确定我是否理解这个难点。如果某些东西返回一个实际的列表,并且它的返回依赖于它,或者它的使用是特定的,那么它应该返回List
#2
10
It sounds to me like you're looking at some poor quality code.
听起来像你在看一些质量差的代码。
Returning IList<T>
rather than List<T>
allows your code to be more flexible. You can replace the implementation with any collection that implements IList<T>
without breaking any calling code. That's a good thing...but only when the functionality defined in IList<T>
matches your needs.
返回IList
You can generalize that to say that you should always return the most generic type possible. In most cases, you can get away with IEnumerable<T>
but if you need more functionality, then IList<T>
works. If that doesn't cut it, return the concrete type and be done with it.
您可以概括地说,您应该始终返回最通用的类型。在大多数情况下,您可以使用IEnumerable
In both of the situations you mention, there is a need to use something not directly provided by IList<T>
(and if there's not, then both of those methods are in error). In those cases, either the method should return List<T>
to provide the functionality needed or the caller should be using another method.
在你提到的两种情况中,都需要使用IList
#3
1
Really you should return IEnumerable if possible, otherwise IList.
真的,如果可能你应该返回IEnumerable,否则IList。
Th reason is that you may want to use something else than a List in the future. It is not uncommon to implement your own list that implements IList and then you do not need to change most of your code.
原因是您可能希望将来使用除List之外的其他内容。实现自己的实现IList的列表并不常见,您不需要更改大部分代码。
Search for derived types from IList and you see that you get many hits just within the .NET framework!
从IList中搜索派生类型,您会发现在.NET框架内获得了很多命中!
#4
1
The idea is to let the caller decide what collection they'll use. You see a lot of people returning IEnumerable<T>
whenever they can. It's generally considered good practice to do so. Returning IList let's the caller use whichever implementation of IList they prefer when returning List requires them to manually copy the List's data in their data collection of choice.
我们的想法是让来电者决定他们将使用哪些收藏品。你会看到很多人随时都会返回IEnumerable
Returning IEnumerable is ideal.
返回IEnumerable是理想的。