List.FindAll 的结果是否保证与原始列表的顺序相同?
如果我有一个包含以下条目的列表:
Apple 香蕉 葡萄 樱桃 橙子 猕猴桃
的结果
fruit.FindAll(f => f.Length == 6)
是保证永远是
香蕉 樱桃 橙色
或者顺序可以不同吗?
If I've got a List with the following entries:
Apple
Banana
Grape
Cherry
Orange
Kiwi
Is the result of
fruit.FindAll(f => f.Length == 6)
guaranteed to always be
Banana
Cherry
Orange
or could the order be different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从某种意义上说,它并没有在文档中说明,但如果您查看它当前的实现方式,那么是的,它总是会以相同的顺序返回。
目前的实现方式如下:
如您所见,这是一个简单的 for 循环,顺序遍历列表,并添加匹配的项目。
It's not guaranteed in the sense that it doesn't say it in the documentation, however if you look at how it's currently implemented, then yes, it'll always come back in the same order.
Here's how it's currently implemented:
As you can see, it's a simple for loop that sequentially goes through the list, and adds the items that match.
当前的实现将保留顺序。
话虽如此,文档中没有任何内容可以保证这一点将始终保留顺序。从理论上讲,未来的版本可能会对该例程或其他类似的函数进行线程化,这会破坏顺序。我不会依赖保留的顺序。
The current implementation will preserve ordering.
That being said, there is nothing in the documentation which guarantees that this will always preserve ordering. It is possible that a future version could, theoretically, thread this routine, or some other similar function, which would break the ordering. I would not rely on the order being preserved.
据我所知,从 List.FindAll 文档中,未指定返回项目的顺序,因此,如果目前指定了,那么这是一个可能会更改的实现细节。
简而言之,是的,顺序可能会有所不同。
As far as I can tell from the List<T>.FindAll documentation, the order of the returned items isn't specified, therefore if it does at present that's an implementation detail which is subject to change.
In short, yes, the order could be different.
List.FindAll
的文档没有明确做出此保证。它确实暗示了它被订购。更重要的是,尽管该方法的实现确实返回了一个有序列表,但我发现很难相信它会被更改为其他任何内容。它只会让太多人崩溃。文档中缺乏明确的措辞可能是一个疏忽。The documentation for
List<T>.FindAll
does not explicitly make this guarantee. It does allude to it being ordered. More importantly though the implementation of the method does return an ordered list and I find it hard to believe it would every be changed to anything else. It would quite simply break too many people. The lack of explicit wording in the documentation is probably an oversight.MSDN 说执行线性搜索,尽管它没有明确表示保证顺序相同。
MSDN says that a linear search is performed, although it doesn't explicitly say that it is guaranteed to be in the same order.