C# IList, ArrayList與List的區別詳解 & 簡單用法舉例

來源:互聯網
上載者:User
留待項目參考:(個人收集)   共同點: IList, List , ArrayList
通俗一點來講就是廣義的數組,C#裡面稱之為集合。不同於一般的狹義的數組,它們可以存放任意類型的東西,在申明或者賦值的時候指定。比如你寫了一個類
Cake,然後想有一個結構來存放很多Cake的執行個體,那就可以用他們。 區別: IList與List 通俗一點講,只能存放同一類型的元素。 比如聲明的時候 聲明為List<Cake>
cakes=new List<Cake>(); 那麼就只能用放cake的執行個體。 在從cakes這個變數中取出元素的時候,取到的直接就是Cake類型。不需要做強行的轉換。 如果想要讓一個‘數組’存放各種類型的執行個體,比如有Cake,Juice, Bread,那麼就用ArrayList ArrayList
food=new ArrayList();注意這裡沒有指定它裝的是什麼類型的元素,所以可以隨便裝咯~當然,好處不能全讓你占完了。在‘取’的時候就要麻煩一點了ArrayList裡元素預設是
Object類型的,所以需要強制轉換一下。
再來說IList和List的區別:我的理解是,IList是一個借口,而List是一個確定的類。介面,當然就需要你去實現它的函數咯,如果你想這些函數有自己的特色,那麼就自己寫一個類去實現吧!然後聲明的時候:IList<類型>
kk=new 你實現的類名<類型>();當然你可以寫成:IList<類型>
kk=new List<類型>();相當於List實現了IList (事實上C#
API中是這樣定義的)
如果你寫成 List<類型>
kk=new List<類型>();那就意味著你的代碼,那些操作List的函數不能有絲毫改變,你得按規定辦事。寫好是什麼,你就用什麼。
用法:以上三個集合的用法都很相似,跟Java也很相似假如有一個 List<Cake>
cakes增、刪、改、查的方法:
  cakes.Add(Cake t);//增 cakes.Remove(int index);//刪 cakes.Remove(Cake t);//刪 cakes[]=//修改的資料 //查或者改 
 更多的操作可以參考C# API

IList和ArrayList操作效能對比 接Killkill:http://blog.csdn.net/killlkilll/archive/2006/12/23/1457022.aspx
Lazy:
http://blog.csdn.net/lazy_/archive/2006/12/24/1458381.aspx

List<T>在建立的時候的時間消耗上明顯比ArrayList要大。
List<T>對實值型別操作不需要進行裝箱;ArrayList需要。

鑒於這兩點
,可以得出,當資料量小的時候呢,ArrayList的操作時間上要比List<T>省,
但是在資料量大的時候呢,List<T>就要比ArrayLIst要省了。

可以來看看下面這個例子:

class
Program
    ...{
        static
void Main(string[]
args)
        ...{
            
Stopwatch sw = new
Stopwatch();

            
sw.Start();
            
IList<SomeType> list
=
new List<SomeType>();
            for
(int i = 0; i
< 1;
i++)
            ...{
                
list.Add(new SomeType(i, "test"));
            
}
            
sw.Stop();
            
Console.WriteLine(sw.Elapsed);

            
sw.Reset();
            
sw.Start();
            
ArrayList al = new
ArrayList();
            for
(int i = 0; i
<1;
i++)
            ...{
                
al.Add(new SomeType(i, "test"));
            
}
           
            
sw.Stop();
            
Console.WriteLine(sw.Elapsed);

            
Console.ReadLine();
        
}
    
}class
SomeType
    ...{
        public
int test_int;
        public
string test_string;

        public
SomeType(int test_int, string
test_string)
        ...{
            this.test_int
=
test_int;
            this.test_string =
test_string;
        
}
    
}

執行結果為:
00:00:00.0005187
00:00:00.0000595

但是當i超過50000條時,大家可以看看執行結果,我在這設定的是1000,0000,其結果為:
Ilist只有
03.8455183
ArrayList 有 20.8369815

using System;
using System.Collections;
using
System.Collections.Generic;

public class MyClass
{
    public
static void Main()
    {
        List<int> iList = new
List<int>();
        int[] iArray = new int[0];
        ArrayList al
= new ArrayList();
        int count = 10;
       
//==========================
        //增加元素
       
//==========================
        for (int i = 0; i < count;
++i)
        {
            iList.Add(i);
        }

       
iArray = new int[count];//需要初始化
        for (int i = 0; i < count;
++i)
        {
            iArray[i] = i;
        }

        for
(int i = 0; i < count; ++i)
        {
           
al.Add(i);//這裡有box操作
        }
       
//==========================
        //輸出
       
//==========================
        foreach (int i in iList)
       
{
            Console.WriteLine(i);
        }

        foreach (int
i in iArray)
        {
            Console.WriteLine(i);
       
}

        foreach (object o in al)
        {
           
Console.WriteLine(o);//這裡有unbox操作
        }
       
//============================
        //繼續增加元素
       
//============================
        iList.Add(count);

       
iArray = new int[count + 1];//需要重新分配記憶體
        for (int i = 0; i < count;
++i)
        {
            iArray[i] = i;
        }
       
iArray[count] = count;

        al.Add(count);
    }
}轉自:http://www.cnblogs.com/Godblessyou/archive/2011/05/05/2037572.html

using System;
using System.Collections;
using
System.Collections.Generic;

public class MyClass
{
    public
static void Main()
    {
        List<int> iList = new
List<int>();
        int[] iArray = new int[0];
        ArrayList al
= new ArrayList();
        int count = 10;
       
//==========================
        //增加元素
       
//==========================
        for (int i = 0; i < count;
++i)
        {
            iList.Add(i);
        }

       
iArray = new int[count];//需要初始化
        for (int i = 0; i < count;
++i)
        {
            iArray[i] = i;
        }

        for
(int i = 0; i < count; ++i)
        {
           
al.Add(i);//這裡有box操作
        }
       
//==========================
        //輸出
       
//==========================
        foreach (int i in iList)
       
{
            Console.WriteLine(i);
        }

        foreach (int
i in iArray)
        {
            Console.WriteLine(i);
       
}

        foreach (object o in al)
        {
           
Console.WriteLine(o);//這裡有unbox操作
        }
       
//============================
        //繼續增加元素
       
//============================
        iList.Add(count);

       
iArray = new int[count + 1];//需要重新分配記憶體
        for (int i = 0; i < count;
++i)
        {
            iArray[i] = i;
        }
       
iArray[count] = count;

        al.Add(count);
    }
}

相關文章

聯繫我們

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