2011-08-14 27 views
9

I.E.如果我想從數組中選擇,是否必然按順序產生對象IEnumerable<object>C#集合是否總是強制執行?

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? 
} 

謝謝!

+0

的條目適用於保證排序順序保證什麼是學生?一個列表?一個排序列表?數組?一本字典? 「藏品」是一個廣義的術語,它們具有不同的特徵。 –

+0

@Mystere:按照第二行,它是一個數組。 –

+0

@Jon Skeet - Doh!錯過了...... –

回答

15

是的,在情況:

  • 陣列返回的自然順序項按照原來的順序的順序
  • Enumerable.Select返回項(投影后,當然)

有些收藏品不是保留訂單,但是。特別是:

  • 集合,例如HashSet<T>Dictionary<TKey, TValue>作出任何有關其值返回
  • 集合,如SortedSet<T>SortedDictionary<TKey, TValue>基於放置在他們
相關問題