.net 集合詳解

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

IEnumerable介面和IEnumerator介面區別

    1. 簡單來說IEnumerable是一個聲明式的介面,聲明實現該介面的類就是“可迭代的enumerable”,但並沒用說明如何?迭代器(iterator).其代碼實現為:

         public interface IEnumerable
         {
                IEnumerator GetEnumerator();
          }
    2. 而IEnumerator介面是實現式介面,它聲明實現該介面的類就可以作為一個迭代器iterator.其代碼實現為:

        public interface IEnumerator
       {
                object Current { get; }

                bool MoveNext();
                void Reset();
        }

 

ICollection繼承自IEnumerable,IList繼承自ICollection

 

這兩個介面都是為了給集合提供一些公用的方法。只是分了兩個層次,IList比ICollection多幾個方法,增加,移除成員。可以簡單理解為:ICollection主要針對靜態集合;IList主要針對動態集合。

 

IQueryable 介面

它繼承 IEnumerable 介面,而因為.net版本加入Linq和IQueryable後,使得IEnumerable不再那麼單調,變得更加強大和豐富。

為了區別兩個介面,我們通過一個實際的例子來解釋一下。

static void Main(string[] args)
        {
            //建立資料庫訪問網關
            using (SchoolDBEntities schoolEntities = new SchoolDBEntities())
            {
                //查詢的結果放入IQueryable介面的集合中
                IQueryable<T_Class> classesIQue = (from c in schoolEntities.T_Class
                                                   orderby c.ID
                                                     select c).Skip<T_Class>(3).Take<T_Class>(3);
                //注意這個AsEnumerable<T_Class>()在分頁查詢之前,先將其轉換成IEnumerable類型
                IEnumerable<T_Class> classesIEnu = (from c in schoolEntities.T_Class
                                                    orderby c.ID  
                                                    select c).AsEnumerable<T_Class>().Skip<T_Class>(3).Take<T_Class>(3);
                //因為啟用了消極式載入機制,所以下面調用一下,才會真正去讀取資料庫
                int i = 0;
                foreach (var c in classesIQue)
                {
                    i++;
                }
                Console.WriteLine(i);
                foreach (var c in classesIEnu)
                {
                    i++;
                }
                Console.WriteLine(i);
            }
            Console.WriteLine("OK");
            Console.ReadKey();
        }

注意紅色代碼部分,在用linq查詢實體集合之前我先將其轉換成 IEnumerable介面類型,看看最終執行的sql是怎樣的。

第一種:直接返回 IQueryable類型的查詢,如所示:

第二種:在用分頁查詢之前先將其轉換成 IEnumerable實際執行的sql如所示:

 

總結

IQueryable介面與IEnumberable介面的區別:  IEnumerable<T> 泛型類在調用自己的SKip 和 Take 等擴充方法之前資料就已經載入在本地記憶體裡了,而IQueryable<T> 是將Skip ,take 這些方法運算式翻譯成T-SQL語句之後再向SQL伺服器發送命令,它並不是把所有資料都載入到記憶體裡來才進行條件過濾。

 

IDictionary 實現是鍵/值對的集合,如 Hashtable 類。IList 實現是值的集合,其成員可通過索引訪問,如 ArrayList 類。

 

IEnumerable <- IEnumerator <- ICollection <- IList <-ArrayList

IEnumerable <- IQueryable

IEnumerable <- IEnumerator <- ICollection <- IDictionary <- Hashtable

 

.net 集合詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.