IEnumerable 、IList 差別

  • 4605
  • 0
  • C#
  • 2020-03-02

 

遇到情境:

把資料排序完後用 IEnumerable 集合容器 存入Cache 中,下次取出才發現資料怎麼亂掉了沒有依照當初的排序,上網查一下後才知道是對集合容器不夠認識所以才導致這次的問題。

IEnumerable 、IList 基本認識

按照功能排序:List<T> 〈  IList<T>   ICollection<T>  IEnumerable<T>

按照性能排序:IEnumerable<T>《ICollection<T>《IList<T>《List<T>

 

IList MSDN文件:

https://msdn.microsoft.com/zh-tw/library/system.collections.ilist(v=vs.110).aspx

       MSDN 解釋:代表可依索引個別存取的非泛型物件集合。     

       ※存入容器的資料每筆都具有index

 

IEnumerable  MSDN 文件:

https://msdn.microsoft.com/zh-tw/library/system.collections.ienumerable(v=vs.110).aspx

       MSDN 解釋:公開能逐一查看非泛型集合內容一次的列舉程式。   

       ※存入容器的資料每筆不具有index

結論:

IList 與 IEnumerable  差別:

IList有索引所以資料會依存進去的索引順序排列,IEnumerable沒有所以所以資料不會依存進去的順序排列

 

如有理解錯誤請各位資訊先進指正~~

 

參考連結:

https://www.cnblogs.com/sunliyuan/p/5816666.htm

https://stackoverflow.com/questions/20160943/comparison-between-list-ilist-and-ienumerable