本文對經常使用的數據結構:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryable, IEnumerable等進行詳述。mysql
1、Collection(集合)Collection是數據記錄集合,編寫代碼過程當中,經常須要合適的容器保存臨時數據,方便修改和查找,如何選取合適的數據容器,關鍵在於將執行的數據操做以及數據記錄是否大量。
算法
2、Array(數組)sql
特徵數據庫
1. 固定大小,數組的大小是初始化時決定沒法修改的數值。數組
2. 強類型,存儲數據元素類型必須在初始化時指定,所以在運行時,不須要耗費額外的時間來定義數組類型,可以大大提高運行效率。服務器
3. 可以使用Foreach關鍵字實現數組迭代和查找。數據結構
由於數組大小是固定的,且是強類型數據結構,所以在運行時只佔用不多的內存,運行時效率很高。ide
3、ArrayList函數
1. ArrayList 沒有固定的長度,容量可動態增長,可應用於開發人員沒法肯定數組元素個數等場景,固然這種狀況下,在定義結構體的時候會很是耗時。性能
2. ArrayList 不是強類型,ArrayList中不一樣元素類型能夠不相同,而且須要在運行時根據實際的輸入來肯定元素類型。所以在運行時消耗內存較多。
3. 可以使用Froeach 關鍵字操做ArrayList。
ArrayList支持String,int,以及十進制小數類型。
4、HashTable(哈希表)
HashTable是一種定義關鍵字的數據結構體,使用哈希表查找數據很是方便,哈希表既不是強類型也不固定大小限制。
5、Stack
棧是最典型的數據結構,棧具備優先級劃分的數據結構,棧爲每一個內容項定義優先級,表示每一個Item入棧和出棧的優先順序。所以操做棧中的數據,須要先將數據push 到棧的頂部,須要刪除元素必須變成棧頂部,即要遵照後進先出(LIFO)的原則。
棧與哈希表同樣既不是強類型也不限制元素個數。
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();
複製代碼
6、Queue
Queue同棧同樣也是具備優先級定義的結構體,遵循的規則是先進先出(FIFO),既不是強類型也不具備固定的大小限制。
入隊操做
//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();
複製代碼
出隊操做
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 BEGINING of the Queue --> First in First out FIFO
this.lblQueue.Text=objQueue.Dequeue().ToString();
this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
複製代碼
入隊操做
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 BEGINING of the Queue --> First in First out FIFO
this.lblQueue.Text=objQueue.Dequeue().ToString();
this.ListBoxQueue.DataSource = objQueue.ToArray();
this.ListBoxQueue.DataBind();
複製代碼
7、List
什麼狀況下須要使用List?
1. List長度可不固定
2. 當數據爲通用類型,List是強類型,List中元素類型不須要等到運行時來肯定,這種特性使得List 運行時效率很是高。
3. 可以使用Foreach關鍵字。
由於List不須要設定固定的大小,List靈活度高,且效率高經常使用於開發過程當中。
//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();
複製代碼
8、IList
IList 繼承了List,包含多種方法的List接口。若是你沒法判斷代碼改動的可能性,可使用IList接口,減小模塊之間的依賴性。IList是接口所以沒法被實例化,因此必須使用List來初始化。
//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();
複製代碼
咱們一塊兒瞭解一下具體的類和接口之間的區別。
1. 具體類可繼承其餘類,並實現一個或多個接口。
2. 在內部類中能夠定義變量並賦值,接口中不容許此操做。
3. 具體類可包含構造函數,而接口中不能定義構造函數
4. 抽象類中可包含訪問修飾符如public,private等,接口中不能包含。
//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();
複製代碼
9、IEnumerable
IEnumerable經常使用於遍歷集合元素,可是沒法修改(刪除或添加)數據,使用IEnumberable 會從服務器端將全部數據拷貝到客戶端,並進行必定的過濾,若是服務器端有大量數據會形成內存負載超重。
//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();
複製代碼
10、IQueryable
IQueryable與IEnumberable不一樣的是,當從服務器端加載過量的數據,IQueryable會自動減小應用負載。IQueryable可保證大數據量時應用程序的高性能。IQueryable會先過濾數據,而後發送給客戶端。
DataAccessEntities ctx = new DataAccessEntities();
var ctx = new DataAccessEntities();
複製代碼
11、SQL Profiler:
如何追蹤查詢語句生成TSQL,生成須要的數據結構體:
Step 1:
Start -> MS SQL Server 2008 -> Performance Tools -> SQL Server Profiler
複製代碼
Step 2:
SQL Server Profiler -> File -> New Trace
複製代碼
Step 3:
輸入鏈接數據庫的用戶名和密碼
Step 4:
General (Tab) -> Use the Template: Standard
複製代碼
Step 5:
Event Selection (Tab) -> Event : TSQL -> Select : SQL-BatchCompleted | Select Show all Columns
Press Column Filter -> Database Name: Like: "DataAccess"
複製代碼
運行
Step 6:
查看結果
Step 7:
生成 IEnumerable數據 :
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Age] AS [Age]
FROM [dbo].[Employee] AS [Extent1]
複製代碼
生成 IQueryable :
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Name] AS [Name],
[Extent1].[Age] AS [Age]
FROM [dbo].[Employee] AS [Extent1]
WHERE 1 = [Extent1].[ID]
複製代碼
ICollection 繼承了IEnumberable,可是IEnumberable是基於索引的,ICollection不基於索引。
12、Stack Generic
入棧:
//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();
複製代碼
Queue Generic
入隊:
//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();
複製代碼
十3、Dictionary 及 IDictionary:
Dictionary 可通用,而哈希表不是通用的。Dictionary定義 <TKey,Tvalue>。IDictionary是Dictionary的接口,若是在後期開發中須要大量修改,建議使用IDictionary。
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,>