C# 集合总是强制执行顺序吗?
IE 如果我想从数组中进行选择,生成的 IEnumerable
public class Student { public string FullName, ... }
public class School { public string Name, public Student[] Students, ... }
public void StudentListWork(School thisSchool)
{
IEnumerable<string> StudentNames = thisSchool.Students.Select(student => student.FullName);
// IS StudentNames GUARANTEED TO BE IN THE SAME ORDER AS thisSchool.Students?
}
谢谢!
I.E. If I want to select from an array, is the resultant IEnumerable<object>
object necessarily in order?
public class Student { public string FullName, ... }
public class School { public string Name, public Student[] Students, ... }
public void StudentListWork(School thisSchool)
{
IEnumerable<string> StudentNames = thisSchool.Students.Select(student => student.FullName);
// IS StudentNames GUARANTEED TO BE IN THE SAME ORDER AS thisSchool.Students?
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,在这种情况下:
Enumerable.Select
按原始序列的顺序返回项目(当然是在投影之后),但是有些集合不保留顺序。特别是:
HashSet
和Dictionary
等集合不保证返回值的SortedDictionary
根据放置在其中的项目应用保证排序Yes, in this case:
Enumerable.Select
returns items in the order of the original sequence (after projection, of course)Some collections do not retain order, however. In particular:
HashSet<T>
andDictionary<TKey, TValue>
make no guarantees about the order in which values are returnedSortedSet<T>
andSortedDictionary<TKey, TValue>
apply guaranteed ordering based on the items placed within them