是对象的顺序返回FOREACH稳定吗?顺序、对象、稳定、FOREACH

2023-09-04 23:40:19 作者:南笙酒味

它是安全的假设两个itterations在相同的集合将返回对象以相同的顺序?显然,假设集合工作尚未改变。

Is it safe to assume that two itterations over the same collection will return the objects in the same order? Obviously, it is assumed that the collection has not otherwise been changed.

推荐答案

简短的回答 - 是的,

Short answer - yes.

显然,虽然,这两个项目集合中的顺序可能不完全,因为它们被插入,这取决于集合的类型(一个字典,例如)。

Obviously, though, the order of the items in the collection may not be exactly as they were inserted, depending on the type of collection (a dictionary, for example).

但你会使用foreach循环每次迭代一个单一的,未经修改的收集时间得到相同的结果。

But you will get the same results each time you iterate over a single, unmodified collection using a foreach loop.