难道C#集合始终维持秩序? [英] Do C# collections always enforce order?

查看:160
本文介绍了难道C#集合始终维持秩序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即。如果我想从一个数组选择,为结果的IEnumerable<对象> 对象一定是为了

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!

推荐答案

是的,在的这个的情况:


  • 阵列返回项目自然秩序

  • Enumerable.Select 返回(当然投影后)在原序列的订单项目

一些藏品做的的保留顺序,但是。特别是:

Some collections do not retain order, however. In particular:


  • 集合,如的HashSet< T> 字典< TKEY的,TValue> 不作任何关于该值返回

  • 集合,如的SortedSet的量级担保; T> SortedDictionary< TKEY的,TValue> 应用基于放置在他们的项目保证排序

  • Collections such as HashSet<T> and Dictionary<TKey, TValue> make no guarantees about the order in which values are returned
  • Collections such as SortedSet<T> and SortedDictionary<TKey, TValue> apply guaranteed ordering based on the items placed within them

这篇关于难道C#集合始终维持秩序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆