C#集合总是强制执行订单吗?

IE如果我想从数组中选择,生成的IEnumerable对象是否必然按顺序排列?

 public class Student { public string FullName, ... } public class School { public string Name, public Student[] Students, ... } public void StudentListWork(School thisSchool) { IEnumerable StudentNames = thisSchool.Students.Select(student => student.FullName); // IS StudentNames GUARANTEED TO BE IN THE SAME ORDER AS thisSchool.Students? } 

谢谢!

是的,在这种情况下:

  • 数组以自然顺序返回项目
  • Enumerable.Select按原始序列的顺序返回项目(当然,在投影之后)

但是,有些馆藏保留订单。 特别是:

  • 诸如HashSetDictionary集合不保证返回值的顺序
  • SortedSetSortedDictionary等集合根据放置在其中的项目应用保证排序