close

1.IEnumerable and IEnumberable<T> 只定義了最基本的集合操作,至於像取得集合元素總數,透過索引取得特定元素等等操作,

就是定義於ICollection,ILIst,IDictionary...介面中。(這些介面也都有泛型版本,且一般而言都該用泛型版本,才有型別安全之好處)

 

下圖為這些基礎介面的繼承關係:

擷取  

 

其中ICollection<T>:可以取得集合元素之總數,判斷某元素是否存於集合中....

底下為其介面定義:

public interface ICollection<T> : IEnumerable<T> ,IEnumerable

{

   int Count{ get; }

   bool Cotains (T item)

   void CopyTo (T[] array , int arrayIndex )

   bool IsReadOnly { get; }

   void Add(T item);

   bool Remove (T item);

   void Clear(); 

}

由上述可之ICollection<T> 繼承 IEnumerable<T>,所以此介面之類別也具有透過foreach來尋訪集合之能力

 

NET Framework提供之泛型集合類別都直接或間接的實做了ICollection<T>介面 , 底下這張圖可以幫助我扪對於

整個集合家族之理解。

(此圖為泛型版本)

擷取  

 

以上參考 Michael Tasi Blog

 

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 melomelo1988 的頭像
    melomelo1988

    melo 唐

    melomelo1988 發表在 痞客邦 留言(0) 人氣()