我为什么要在List 上返回IList ? [重复]

时间:2021-11-12 06:22:52

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 而不是List ,但我找不到任何真正好的理由。我一直在寻找执行此操作的代码,然后调用代码通常执行以下两项操作之一:

  1. Call new List<T>(returnedIList) so it can use all the nice methods on List
  2. 调用new List (returnedIList),以便它可以使用List上的所有好方法

  3. Casts back to List<T> so it can use all the nice methods on List
  4. 转换回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 并且出于某种原因必须用IList 的实现替换它,我无法从List 继承,那么我将得到构建错误并且必须更改一些代码。这可能是非常不可能的,如果发生这种情况,修复工作并不是很多。当然,不值得失去List 的好处和/或不得不抛出/新的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 的任何东西,而不仅仅是List。但是,更糟糕的是,自Linq问世以来,我已经开始制作很多东西,返回Enumerable 甚至只是IEnumerable!

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 。如果没有,那么你应该没有必要将它强制转换为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 而不是List 可以使代码更灵活。您可以使用任何实现IList 的集合替换实现,而不会破坏任何调用代码。这是一件好事...但只有在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 ,但如果您需要更多功能,那么IList 可以正常工作。如果没有切割它,返回具体类型并完成它。

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 不直接提供的东西(如果没有,那么这两种方法都是错误的)。在这些情况下,该方法应该返回List 以提供所需的功能,或者调用者应该使用另一种方法。

#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 。这通常被认为是良好的做法。返回IList让调用者在返回List时使用他们喜欢的IList的任何实现,List要求他们在他们选择的数据集合中手动复制List的数据。

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 的任何东西,而不仅仅是List。但是,更糟糕的是,自Linq问世以来,我已经开始制作很多东西,返回Enumerable 甚至只是IEnumerable!

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 。如果没有,那么你应该没有必要将它强制转换为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 而不是List 可以使代码更灵活。您可以使用任何实现IList 的集合替换实现,而不会破坏任何调用代码。这是一件好事...但只有在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 ,但如果您需要更多功能,那么IList 可以正常工作。如果没有切割它,返回具体类型并完成它。

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 不直接提供的东西(如果没有,那么这两种方法都是错误的)。在这些情况下,该方法应该返回List 以提供所需的功能,或者调用者应该使用另一种方法。

#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 。这通常被认为是良好的做法。返回IList让调用者在返回List时使用他们喜欢的IList的任何实现,List要求他们在他们选择的数据集合中手动复制List的数据。

Returning IEnumerable is ideal.

返回IEnumerable是理想的。