天天看點

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

目錄

集合

數組

數組清單

哈希表

堆棧

隊列

清單

IList

具體類與接口的差別

IEnumerable

IQueryable

SQL事件探查器

如何跟蹤查詢生成TSQL和将加載多少條記錄:

ICollection

泛型堆棧

Push:

Pop:

泛型隊列

入隊:

出隊:

字典和IDictionary

參考

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

集合

集合是相關記錄集。它包含一個有意義的單元:  

我們應該選擇适當的容器來臨時存儲資料以進行擷取和修改過程。 

适當的容器取決于:

1、我們想要對資料做的目标(隻是閱讀、進行插入、删除、更新等修改)  

2、應該轉移的記錄數量

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

數組

1.固定長度 - >它的大小不靈活。它是在執行個體化時确定的。

 2.強類型 - >開發人員在執行個體化時确定其類型而不是運作時。此功能使得在運作時快速運作,因為它不需要等待類型定義。 

 3.開發人員使用“foreach”關鍵字來填充和周遊數組。

固定長度和強類型消耗較少的記憶體,是以它具有良好的性能。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//It is obvious that strArray is
//1. string   --> Strongly Type
//2. Sized=10 --> Fixed Size

string[] strArray = new string[10];

for (int i = 0; i < 10; i++)
{
    if (strArray[i]==null)
    {
        strArray[i] = (i+1).ToString();
    }
}

this.ListBoxArray.DataSource = null;
this.ListBoxArray.Items.Clear();

this.ListBoxArray.DataSource = strArray;
this.ListBoxArray.DataBind();
           

數組清單

1.

Arraylist

 不是固定長度 - >資料有可能增加。一方面,當開發人員不确定

arraylist

 的大小時,它是一個很好的功能,另一方面,它可能需要很長時間來定義大小。

2.

Arraylist

 不是強類型 - >每當開發人員不确定輸入或輸出資料的确切類型定義時,他們應該等到運作時确定它的類型。它的缺點是在運作時為記憶體确定類型定義而耗費的時間。

3.開發人員使用“foreach”關鍵字來填充和周遊arraylist。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
public class Product
    {
        public Product()
        {
        
        }
        public Product(string Code, string Name)
        {
            _Code = Code;
            _Name = Name;
        }

		    public string _Code {get; set;}
            public string _Name { get; set; }
    }
           

ArrayList

 可以同時接受string,整數和十進制。

//It is NOT obvious that strArrayList is 1. string? int? object? decimal?  --> NOT Strongly Type
//                                       2. Sized=10? 20? 100?             -->NOT Fixed Size
// Namespace: System.Collections

System.Collections.ArrayList strArrayList = new System.Collections.ArrayList();
//System.Linq.IQueryable  type of data is not specific runtime deferred support
strArrayList.Add("Mahsa");  //   "Mahsa": is string
strArrayList.Add(1);        //        1 : is integer
strArrayList.Add(0.89);     //      0.89: is decimal

this.ListBoxArrayList.DataSource = null;
this.ListBoxArrayList.Items.Clear();
this.ListBoxArrayList.DataSource = strArrayList;
this.ListBoxArrayList.DataBind();

System.Text.StringBuilder str= new System.Text.StringBuilder();

foreach (var item in strArrayList)
{
    str.Append(" , "+item);
}
this.lblArrayList.Text = str.ToString();

//Below is old way to fill obj from product,
//in Arraylist you need to create more than one instance
// Product objProduct = new Product();
// objProduct.Code = "1001";
// objProduct.Name = "Chair";

//It is NOT obvious that strArrayList is
//1. string? int? object? decimal? OR OBJECT??  --> NOT Strongly Type
//2. Sized=10? 20? 100?                         -->NOT Fixed Size
// Namespace: System.Collections

System.Collections.ArrayList objArrayList = new System.Collections.ArrayList();

objArrayList.Add(new Product("1001", "Chair"));
objArrayList.Add(new Product("1002", "Sofa"));
objArrayList.Add(new Product("1003", "Carpet"));

this.DropDownListArrayListObject.DataSource = null;
this.DropDownListArrayListObject.Items.Clear();
this.DropDownListArrayListObject.DataSource = objArrayList;

//* Finding among Object of Array List is difficult,
//you have to find your specific item by index
Product objTemp = (Product)objArrayList[0];
objArrayList.Remove(objTemp);
//*
this.DropDownListArrayListObject.DataTextField = "_Name";
this.DropDownListArrayListObject.DataValueField = "_Code";
this.DropDownListArrayListObject.DataBind();
this.GridViewArrayListObject.DataSource = objArrayList;
this.GridViewArrayListObject.DataBind();
           

哈希表

HashTable

 是另一種資料結構,它定義每個資料部分的鍵值。是以,隻需指出資料的鍵就很容易找到資料。它不是強類型,也不是固定大小。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//It is NOT obvious that strArrayList is
//1. string? int? object? decimal? OR OBJECT??  --> NOT Strongly Type
//2. Sized=10? 20? 100?                         -->NOT Fixed Size
// Namespace: System.Collections
//Hashtable solve the problem in Arraylist when we are looking for specific item
//Hashtable dedicate a key for each item, then finding item is easier and faster

 System.Collections.Hashtable objHashTable = new System.Collections.Hashtable();

 objHashTable.Add("1001","Chair");
 objHashTable.Add("1002", "Sofa");
 objHashTable.Add("1003", "Carpet");


this.DropDownListHashTable.DataSource = null;
this.DropDownListHashTable.Items.Clear();
this.DropDownListHashTable.DataSource = objHashTable;
//* finding item is easier you just need to point to it by call its key
objHashTable.Remove("1002");
//*
this.DropDownListHashTable.DataTextField = "Value";
this.DropDownListHashTable.DataValueField = "Key";
this.DropDownListHashTable.DataBind();
           

堆棧

我們有不同的資料結構,堆棧就是其中之一。堆棧(

Stack

)是資料結構的子集。堆棧(

Stack

)是優先資料結構(例如List是索引基數)。堆棧(

Stack

)定義每個項目的優先級,這意味着堆棧行為強制其項目放入(推送)堆棧優先級形式。是以堆棧将後面的項目放在項目的頂部,這種行為是“為每個項目定義優先級”。是以,無論何時要插入項目,都應在堆棧頂部添加(PUSH),并且每當要從堆棧中删除(POP)項目時,都應将其從堆棧頂部删除。當你得到它時,最後一個項目将被選為第一個POP,它在計算機科學中的表達等于“後進先出(Last in First out)”==“LIFO”。

它不是強類型,也不是固定大小。 

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

堆棧——推(Push)

//Stack is LIFO: Last in First Out
System.Collections.Stack objStackPush = new System.Collections.Stack();

//By Push method, you can insert item at the top of the stack
objStackPush.Push("Mahsa");
objStackPush.Push("Hassankashi");
this.lblPop.Text = "";
this.ListBoxStack.DataSource = objStackPush.ToArray();
this.ListBoxStack.DataBind();
           

堆棧——删除(Pop)

System.Collections.Stack objStackPop = new System.Collections.Stack();

objStackPop.Push("Mahsa");
objStackPop.Push("Hassankashi");

//By Pop method, you can remove item from the top of the stack --> Last in First in
this.lblPop.Text = objStackPop.Pop().ToString();

this.ListBoxStack.DataSource = objStackPop.ToArray();
this.ListBoxStack.DataBind();
           

隊列

隊列(

Queue

)是另一種資料結構,它以其他形式定義每個項目的優先級。是以,無論何時要插入項目,都應在隊列的頭部添加(Enqueue),并且每當要從隊列中删除(Dequeue)項目時,都應将其從隊列的底部删除。當你得到它時,第一個出現的項目将被選擇為第一個出列,并且它在計算機科學中的表達等于“先入先出(First in First out)”==“FIFO”。

它不是強類型,也不是固定大小。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

隊列——入隊(Enqueue)

//Queue is FIFO: First in First Out
System.Collections.Queue objQueue = new System.Collections.Queue();

//By Enqueue method you can insert item at the END of the Queue
objQueue.Enqueue("Mahsa");
objQueue.Enqueue("Hassankashi");
objQueue.Enqueue("Cosmic");
objQueue.Enqueue("Verse");

this.lblQueue.Text = "";
this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
           

隊列——出隊(Dequeue)

System.Collections.Queue objQueue = new System.Collections.Queue();

objQueue.Enqueue("Mahsa");
objQueue.Enqueue("Hassankashi");
objQueue.Enqueue("Cosmic");
objQueue.Enqueue("Verse");

//By Dequeue method you can remove item from the BEGINNING of the Queue -->
//First in First out FIFO
this.lblQueue.Text=objQueue.Dequeue().ToString();

this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
           

清單

為什麼我們需要List?

1.清單(List)不是固定長度 - >資料可能會增加。一方面,當開發人員不确定

arraylist

的大小時,它是一個很好的功能,另一方面,它可能需要很長時間來定義大小。

2.當清單(List)被定義為“泛型”時,清單是強類型的 - >每當開發人員确定輸入或輸出資料的确切類型定義時,他們不會等到運作時出現其類型。此功能使得在運作時快速運作,因為它不需要等待類型定義。 

3.開發人員使用“foreach”關鍵字來填充和周遊數組。

由于

List

 不是固定長度,是以開發人員可以靈活地使用它,因為它在定義為“泛型”時是強類型的,是以我們的代碼在運作時運作得很快,因為它不需要等待類型定義。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//Like Array is Strong Type
//Like ArrayList with No Dimension
System.Collections.Generic.List<string> strList = new List<string>();

strList.Add("Mahsa");
strList.Add("Hassankashi");
strList.Add("Cosmic");
strList.Add("Verse");

this.ListBoxListGeneric.DataSource = strList;
this.ListBoxListGeneric.DataBind();

System.Text.StringBuilder str = new System.Text.StringBuilder();

foreach (var item in strList)
{
    str.Append(" , " + item);
}
this.lblList.Text = str.ToString();
           

IList

為什麼我們需要IList?IList由List實作,IList是一個接口并實作方法。每當您估計将來代碼更改的可能性時,您必須使用IList,因為接口會降低依賴性,并且隻需稍加修改即可運作代碼。是以,您應該觀察多态性,以便在添加或删除可能更改的方法時将應用程式和控制分離。其他一切都很相似。每當我們想要改變某些操作時,是以“IList”允許我們至少在改變整個代碼時輕松地做到這一點。 

接口無法執行個體化,是以應該從List執行個體化

System.Collections.Generic.IList<string> strIList = new List<string>();
           

具體類與接口的差別

1. 具體類隻從一個類繼承,但它可以實作一個或多個接口

2.你可以在具體類中編寫函數的完整版,而你必須在接口内定義簽名。

3. 您可以在具體類中定義變量和值,而不允許在接口内定義變量。

4.不允許在接口内定義構造函數,然而可以在具體類中定義構造函數  。

5.抽象類可以包含通路修飾符,而接口則不包含通路修飾符  。

正如我提到的,一個類如何不能從兩個類中驅動,它隻能從一個類中驅動,是以每當您想從兩個類中驅動時,都不可能從兩個抽象類繼承,但可以通過接口從多個類中驅動。

将來如果開發人員決定在他們的類上添加一些功能并從另一個類繼承它,開發人員總是喜歡使用集合接口,這樣如果你想改變你的代碼并增強它的能力,選擇接口。

另一方面,接口使程式可擴充和解耦:這些類彼此獨立,是以在将來通過接口更改代碼時很少會發生錯誤,異常和失敗。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

多态性:當你使用接口時,你絕對可以觀察并執行多态和OOP。這意味着封裝你的設計器。接口是指一個點或一個節點将兩個部分互相連接配接起來,這意味着 從兩個部分做出  低依賴關系,并制作一個聯合部分,以便将來進行靈活的變更。

//Ilist can not be instantiate from Ilist , so it should be instantiate from List
System.Collections.Generic.IList<string> strIList = new List<string>();

strIList.Add("Mahsa");
strIList.Add("Hassankashi");
strIList.Add("Cosmic");
strIList.Add("Verse");

this.ListBoxListGeneric.DataSource = strIList;
this.ListBoxListGeneric.DataBind();

System.Text.StringBuilder str = new System.Text.StringBuilder();

foreach (var item in strIList)
{
    str.Append(" , " + item);
}
this.lblList.Text = str.ToString();
           

IEnumerable

IEnumerable

 僅适用于通過集合的疊代,你無法修改(添加或删除)資料

IEnumerable

 将所有資料從伺服器帶到用戶端然後過濾它們,假設你有很多記錄,是以

IEnumerable

 會給你的記憶體帶來開銷。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//IEnumerable can not be instantiate from Enumerable , so it should be instantiate from List
System.Collections.Generic.IEnumerable<Employee> empIEnumerable = new List<Employee>
{   new Employee { ID = 1001, Name="Mahsa"},
    new Employee { ID = 1002, Name = "Hassankashi" },
    new Employee { ID = 1003, Name = "CosmicVerse" },
    new Employee { ID = 1004, Name = "Technical" }
};

this.GridViewIEnumerable.DataSource = empIEnumerable;
this.GridViewIEnumerable.DataBind();

System.Text.StringBuilder str = new System.Text.StringBuilder();

foreach (Employee item in empIEnumerable)
{
    str.Append(" , " + item.ID +"-"+item.Name);
}

this.lblIEnumerable.Text = str.ToString();
           

IQueryable

每當我們遇到包含如此多記錄的大量資料時,我們就必須減少應用程式的開銷。

IQueryable

 首先通過過濾資料然後将過濾後的資料發送到用戶端,在這種情況下(大資料)準備了高性能。 

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
DataAccessEntities ctx = new DataAccessEntities();
        var ctx = new DataAccessEntities();
           
List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//Difference between IQueryable and IEnumerable
//You can instantiate IEnumerable from List
IEnumerable<employee> queryIEnumerable = new List<employee>() ;

//Bring  ALL records from server --> to client then filter collection
//To bring all data from server you should omit where clause from linq to sql
queryIEnumerable = from m in ctx.Employees select m;

//If you use where as extension method with IEnumerable then All records will be loaded
queryIEnumerable = queryIEnumerable.Where(x => x.ID == 1).ToList();

//You cannot instantiate IQueryable

IQueryable<employee> queryIQueryable=null;

//Bring just ONE record from server --> to client

queryIQueryable = (from m in ctx.Employees
             where m.ID == 1
             select m);

//Whenever you call IQueryable so ==> It will be executed
this.GridViewIQueryable.DataSource = queryIQueryable.ToList();
this.GridViewIQueryable.DataBind();
           

SQL事件探查器

如何跟蹤查詢生成TSQL和将加載多少條記錄:

步驟1:

開始 - > MS SQL Server 2008 - >性能工具 - > SQL Server Profiler

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第2步:

SQL Server Profiler - >檔案 - >新跟蹤

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第3步:

用您的使用者名和密碼連接配接。

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第4步:

正常(Tab) - >使用模闆:标準

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第5步:

事件選擇(Tab) - >事件:TSQL - >選擇:SQL-BatchCompleted | 選擇“顯示所有列”

按列過濾器 - >資料庫名稱:類似:“DataAccess” 

按運作

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第6步:

轉到MS SQL Server Management Studio - >計算所有記錄(記錄= 5)

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

第7步:

IEnumerable生成:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name], 
[Extent1].[Age] AS [Age]
FROM [dbo].[Employee] AS [Extent1]
           
List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

IQueryable生成

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[Name] AS [Name], 
[Extent1].[Age] AS [Age]
FROM [dbo].[Employee] AS [Extent1]
WHERE 1 = [Extent1].[ID]
           
List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考

ICollection

ICollection

 繼承自IEnumerable。有一個差別:

你可以找到IEnumerable[ i ] - > Index Based(基于索引)

你找不到

ICollection[ i ]

  - > Not Index Based(不是基于索引)

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary集合數組數組清單哈希表堆棧隊列清單IListIEnumerableIQueryableSQL事件探查器ICollection泛型堆棧泛型隊列字典和IDictionary參考
//IList {indexer and Modify} vs ICollection {randomly and Modify}
//Collection can not be instantiate from ICollection , so it should be instantiate from List
System.Collections.Generic.ICollection<string> strICollection = new List<string>();
strICollection.Add("Mahsa");
strICollection.Add("Hassankashi");

//Countable***
int ICollectionCount=strICollection.Count;

this.ListBoxICollection.DataSource = strICollection;
this.ListBoxICollection.DataBind();
System.Text.StringBuilder str = new System.Text.StringBuilder();
foreach (var item in strICollection)
{
    str.Append(" , " + item);
}
this.lblICollection.Text = str.ToString();

//IList***
System.Collections.Generic.IList<Employee> objIList = new List<Employee>();
objIList = (from m in ctx.Employees
            select m).ToList();

Employee obj = objIList.Where(i => i.Name == "Sara").FirstOrDefault();
int indexofSara= objIList.IndexOf(obj);
int cIList = objIList.Count;

//ICollection***
System.Collections.Generic.ICollection<Employee> objICollection = new List<Employee>();
objICollection = (from m in ctx.Employees
                  select m).ToList();
Employee objIC = objICollection.Where(i => i.Name == "Sara").FirstOrDefault();
//You can not get index of object , if you clear comment from below code appears error
// int indexofSaraICollection = objIC.IndexOf(objIC);
int cICollection = objICollection.Count;
           

泛型堆棧

Push:

//Stack is LIFO: Last in First Out
       //Here is for Push Stack in Generic
       //System.Collections.Stack objStackPush = new System.Collections.Stack();
       //Stack<T> can be instantiated from Stack<T>

       System.Collections.Generic.Stack<int> objStackPush = new System.Collections.Generic.Stack<int>();

       objStackPush.Push(1);
       objStackPush.Push(2);

       this.lblPopGeneric.Text = "";
       this.ListBoxStackGeneric.DataSource = objStackPush.ToArray();
       this.ListBoxStackGeneric.DataBind();
           

Pop:

//Stack is LIFO: Last in First Out
//Here is for Pop Stack in Generic
//System.Collections.Stack objStackPop = new System.Collections.Stack();
//Stack<T> can be instantiated from Stack<T>

System.Collections.Generic.Stack<int> objStackPop = new System.Collections.Generic.Stack<int>();

objStackPop.Push(1);
objStackPop.Push(2);

this.lblPop.Text = objStackPop.Pop().ToString();
this.ListBoxStack.DataSource = objStackPop.ToArray();
this.ListBoxStack.DataBind();
           

泛型隊列

入隊:

//Queue is FIFO: First in First Out
//Here is for Enqueue Queue in Generic
//System.Collections.Queue objQueue = new System.Collections.Queue();
//Queue<T> can be instantiated from Queue<T>

System.Collections.Generic.Queue<int> objQueue = new System.Collections.Generic.Queue<int>();
objQueue.Enqueue(1);
objQueue.Enqueue(2);

this.lblQueue.Text = "";

this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
           

出隊:

//Queue is FIFO: First in First Out
//Here is for Dequeue Queue in Generic
//System.Collections.Queue objQueue = new System.Collections.Queue();
//Queue<T> can be instantiated from Queue<T>

System.Collections.Generic.Queue<int> objQueue = new System.Collections.Generic.Queue<int>();

objQueue.Enqueue(1);
objQueue.Enqueue(2);

this.lblQueue.Text = objQueue.Dequeue().ToString();

this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
           

字典和IDictionary

字典(

Dictionary

)是GENERIC而

HashTable

 不是泛型的:Dictionary<TKey, TValue>,它們之間存在微小的差别,如果字典找不到特定的鍵将抛出異常,而

HashTable

 隻傳回null。

IDictionary

 是接口,如果你估計未來有重大變化,使用

IDictionary

 而不是

Dictionary

 。

//Dictionary can instantiate from Dictionary , Dictionary is similar to Hashtable,
//Dictionary is GENERIC but Hashtable is NON GENERIC
//Such Hashtable you can find object by its key
System.Collections.Generic.Dictionary<int,
string=""> objDictionary = new Dictionary<int, string="">();

objDictionary.Add(1001, "Mahsa");
objDictionary.Add(1002, "Hassankashi");
objDictionary.Add(1003, "Cosmicverse");

string str = objDictionary[1002];

this.ListBoxDictionary.DataSource = objDictionary;
this.ListBoxDictionary.DataBind();</int,></int,>
           

參考

msdn.microsoft.com:常用的集合類型

http://www.codeproject.com/Articles/732425/IEnumerable-Vs-IQueryable

http://www.dotnet-tricks.com/Tutorial/linq/I8SY160612-IEnumerable-VS-IQueryable.html

http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list

http://www.dotnetperls.com/

原文位址:https://www.codeproject.com/Articles/832189/List-vs-IEnumerable-vs-IQueryable-vs-ICollection-v

繼續閱讀