【轉】List<T>和ILIst<T>的區別

來源:互聯網
上載者:User

標籤:泛型類   pac   where   html   ret   欄位   指定   區別   tar   

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace List{    public class Users  //類Users  使用者    {        public string Name; //  姓名        public int Age;  //  年齡        public Users(string _Name, int _Age)        {            Name = _Name;            Age = _Age;        }    }           class Program    {        static void Main(string[] args)        {            Users U = new Users("jiang", 24);                        IList<Users> UILists = new List<Users>();  //千萬要注意:等式的右邊是List<Users>,而不是 IList<Users>,//如果在List前面加一個I, 就會出現錯誤:抽象類別或介面無法建立執行個體。                        UILists.Add(U);            U = new Users("wang", 22);            UILists.Add(U);            List<Users> I = ConvertIListToList<Users>(UILists);            Console.WriteLine(I[0].Name);            Console.WriteLine(I[1].Name);            Console.Read();        }        // **//// <summary>        /// 轉換IList<T>為List<T>      //將IList介面泛型轉為List泛型型別        /// </summary>        /// <typeparam name="T">指定的集合中泛型的類型</typeparam>        /// <param name="gbList">需要轉換的IList</param>        /// <returns></returns>        public static List<T> ConvertIListToList<T>(IList<T> gbList) where T : class   //靜態方法,泛型轉換,        {            if (gbList != null && gbList.Count >= 1)            {                List<T> list = new List<T>();                for (int i = 0; i < gbList.Count; i++)  //將IList中的元素複製到List中                {                    T temp = gbList[i] as T;                    if (temp != null)                        list.Add(temp);                }                return list;            }            return null;        }    }}

 注意:

 IList<Users> UILists = new List<Users>();  //千萬要注意:等式的右邊是List<Users>,

而不是 IList<Users>,如果在List前面加一個I, 就會出現錯誤:抽象類別或介面無法建立執行個體。

下面說一下IList與List的區別:

(1)首先IList 泛型介面是 ICollection 泛型介面的子代,並且是所有泛型列表的基底介面。

它僅僅是所有泛型型別的介面,並沒有太多方法可以方便實用,如果僅僅是作為集合資料的承載體,確實,IList<T>可以勝任。

不過,更多的時候,我們要對集合資料進行處理,從中篩選資料或者排序。這個時候IList<T>就愛莫能助了。

1、當你只想使用介面的方法時,ILis<>這種方式比較好.他不擷取實現這個介面的類的其他方法和欄位,有效節省空間的.

2、IList <>是個介面,定義了一些操作方法這些方法要你自己去實現
List <>是泛型類,它已經實現了IList <>定義的那些方法

IList <Class1> IList11 =new List <Class1>();
List <Class1> List11 =new List <Class1>();

這兩行代碼,從操作上來看,實際上都是建立了一個List<Class1>對象的執行個體,也就是說,他們的操作沒有區別。

只是用於儲存這個操作的傳回值變數類型不一樣而已。

那麼,我們可以這麼理解,這兩行代碼的目的不一樣。
List <Class1> List11 =new List <Class1>();
是想建立一個List<Class1>,而且需要使用到List<T>的功能,進行相關操作。

IList <Class1> IList11 =new List <Class1>();
只是想建立一個基於介面IList<Class1>的對象的執行個體,只是這個介面是由List<T>實現的。所以它只是希望使用到IList<T>介面規定的功能而已。

 

原文看這裡》》》

【轉】List<T>和ILIst<T>的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.