博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List vs IEnumerable vs IQueryable vs ICollection vs IDictionary
阅读量:3527 次
发布时间:2019-05-20

本文共 15051 字,大约阅读时间需要 50 分钟。

 

目录


集合

集合是相关记录集。它包含一个有意义的单元:  

我们应该选择适当的容器来临时存储数据以进行获取和修改过程。 
适当的容器取决于:
1、我们想要对数据做的目标(只是阅读、进行插入、删除、更新等修改)  
2、应该转移的记录数量

数组

1.固定长度 - >它的大小不灵活。它是在实例化时确定的。

 2.强类型 - >开发人员在实例化时确定其类型而不是运行时。此功能使得在运行时快速运行,因为它不需要等待类型定义。 

 
 3.
开发人员使用foreach关键字来填充和遍历数组。

固定长度和强类型消耗较少的内存,因此它具有良好的性能。

//It is obvious that strArray is//1. string   --> Strongly Type//2. Sized=10 --> Fixed Sizestring[] 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

 

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.CollectionsSystem.Collections.ArrayList strArrayList = new System.Collections.ArrayList();//System.Linq.IQueryable  type of data is not specific runtime deferred supportstrArrayList.Add("Mahsa");  //   "Mahsa": is stringstrArrayList.Add(1);        //        1 : is integerstrArrayList.Add(0.89);     //      0.89: is decimalthis.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.CollectionsSystem.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 indexProduct 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 是另一种数据结构,它定义每个数据部分的键值。因此,只需指出数据的键就很容易找到数据。它不是强类型,也不是固定大小。

//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 keyobjHashTable.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”

它不是强类型,也不是固定大小。 

堆栈——(Push)

//Stack is LIFO: Last in First OutSystem.Collections.Stack objStackPush = new System.Collections.Stack();//By Push method, you can insert item at the top of the stackobjStackPush.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 inthis.lblPop.Text = objStackPop.Pop().ToString();this.ListBoxStack.DataSource = objStackPop.ToArray();this.ListBoxStack.DataBind();

队列

队列(Queue)是另一种数据结构,它以其他形式定义每个项目的优先级。因此,无论何时要插入项目,都应在队列的头部添加(Enqueue),并且每当要从队列中删除(Dequeue)项目时,都应将其从队列的底部删除。当你得到它时,第一个出现的项目将被选择为第一个出列,并且它在计算机科学中的表达等于先入先出(First in First out)”==“FIFO”

它不是强类型,也不是固定大小。

队列——入队(Enqueue)

//Queue is FIFO: First in First OutSystem.Collections.Queue objQueue = new System.Collections.Queue();//By Enqueue method you can insert item at the END of the QueueobjQueue.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 FIFOthis.lblQueue.Text=objQueue.Dequeue().ToString();this.ListBoxQueue.DataSource = objQueue.ToArray();this.ListBoxQueue.DataBind();

列表

为什么我们需要List

1.列表(List)不是固定长度 - >数据可能会增加。一方面,当开发人员不确定arraylist的大小时,它是一个很好的功能,另一方面,它可能需要很长时间来定义大小。

2.当列表(List)被定义为泛型时,列表是强类型的 - >每当开发人员确定输入或输出数据的确切类型定义时,他们不会等到运行时出现其类型。此功能使得在运行时快速运行,因为它不需要等待类型定义。 

3.开发人员使用foreach关键字来填充和遍历数组。

由于List 不是固定长度,因此开发人员可以灵活地使用它,因为它在定义为泛型时是强类型的,所以我们的代码在运行时运行得很快,因为它不需要等待类型定义。

//Like Array is Strong Type//Like ArrayList with No DimensionSystem.Collections.Generic.List
strList = new List
();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

为什么我们需要IListIListList实现,IList是一个接口并实现方法。每当您估计将来代码更改的可能性时,您必须使用IList,因为接口会降低依赖性,并且只需稍加修改即可运行代码。因此,您应该观察多态性,以便在添加或删除可能更改的方法时将应用程序和控制分离。其他一切都很相似。每当我们想要改变某些操作时,所以IList允许我们至少在改变整个代码时轻松地做到这一点。 

接口无法实例化,因此应该从List实例化

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

 

具体类与接口的区别

1. 类只从一个类继承,但它可以实现一个或多个接口

2.你可以在具体类中编写函数的完整版,而你必须在接口内定义签名。
3. 您可以在具体类中定义变量和值,而不允许在接口内定义变量。
4.不允许在接口内定义构造函数,然而可以在具体类中定义构造函数  
5.抽象类可以包含访问修饰符,而接口则不包含访问修饰符  

正如我提到的,一个类如何不能从两个类中驱动,它只能从一个类中驱动,所以每当您想从两个类中驱动时,都不可能从两个抽象类继承,但可以通过接口从多个类中驱动

将来如果开发人员决定在他们的类上添加一些功能并从另一个类继承它,开发人员总是喜欢使用集合接口,这样如果你想改变你的代码并增强它的能力,选择接口。

另一方面,接口使程序可扩展解耦:这些类彼此独立,因此在将来通过接口更改代码时很少会发生错误,异常和失败。

多态性:当你使用接口时,你绝对可以观察并执行多态和OOP。这意味着封装你的设计器。接口是指一个点或一个节点将两个部分相互连接起来,这意味着 从两个部分做出  低依赖关系,并制作一个联合部分,以便将来进行灵活的变更。

//Ilist can not be instantiate from Ilist , so it should be instantiate from ListSystem.Collections.Generic.IList
strIList = new List
();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 会给你的内存带来开销。

//IEnumerable can not be instantiate from Enumerable , so it should be instantiate from ListSystem.Collections.Generic.IEnumerable
empIEnumerable = new List
{ 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 首先通过过滤数据然后将过滤后的数据发送到客户端,在这种情况下(大数据)准备了高性能。 

DataAccessEntities ctx = new DataAccessEntities();        var ctx = new DataAccessEntities();

//Difference between IQueryable and IEnumerable//You can instantiate IEnumerable from ListIEnumerable
queryIEnumerable = new List
() ;//Bring ALL records from server --> to client then filter collection//To bring all data from server you should omit where clause from linq to sqlqueryIEnumerable = from m in ctx.Employees select m;//If you use where as extension method with IEnumerable then All records will be loadedqueryIEnumerable = queryIEnumerable.Where(x => x.ID == 1).ToList();//You cannot instantiate IQueryableIQueryable
queryIQueryable=null;//Bring just ONE record from server --> to clientqueryIQueryable = (from m in ctx.Employees where m.ID == 1 select m);//Whenever you call IQueryable so ==> It will be executedthis.GridViewIQueryable.DataSource = queryIQueryable.ToList();this.GridViewIQueryable.DataBind();

SQL事件探查器

如何跟踪查询生成TSQL和将加载多少条记录:

步骤1

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

 

2步:

SQL Server Profiler - >文件 - >新跟踪

3步:

用您的用户名和密码连接。

4步:

常规(Tab - >使用模板:标准

5步:

事件选择(Tab - >事件:TSQL - >选择:SQL-BatchCompleted | 选择显示所有列

按列过滤器 - >数据库名称:类似:“DataAccess” 

按运行

6步:

转到MS SQL Server Management Studio - >计算所有记录(记录= 5

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

ICollection 继承自IEnumerable。有一个区别:

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

你找不到ICollection[ i ]  - > Not Index Based(不是基于索引)

//IList {indexer and Modify} vs ICollection {randomly and Modify}//Collection can not be instantiate from ICollection , so it should be instantiate from ListSystem.Collections.Generic.ICollection
strICollection = new List
();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
objIList = new List
();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
objICollection = new List
();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
can be instantiated from Stack
System.Collections.Generic.Stack
objStackPush = new System.Collections.Generic.Stack
(); 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
can be instantiated from Stack
System.Collections.Generic.Stack
objStackPop = new System.Collections.Generic.Stack
();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
can be instantiated from Queue
System.Collections.Generic.Queue
objQueue = new System.Collections.Generic.Queue
();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
can be instantiated from Queue
System.Collections.Generic.Queue
objQueue = new System.Collections.Generic.Queue
();objQueue.Enqueue(1);objQueue.Enqueue(2);this.lblQueue.Text = objQueue.Dequeue().ToString();this.ListBoxQueue.DataSource = objQueue.ToArray();this.ListBoxQueue.DataBind();

字典和IDictionary

字典(Dictionary)GENERICHashTable 不是泛型的: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 keySystem.Collections.Generic.Dictionary
objDictionary = new Dictionary
();objDictionary.Add(1001, "Mahsa");objDictionary.Add(1002, "Hassankashi");objDictionary.Add(1003, "Cosmicverse");string str = objDictionary[1002];this.ListBoxDictionary.DataSource = objDictionary;this.ListBoxDictionary.DataBind();

参考

 

原文地址:

转载地址:http://jjzhj.baihongyu.com/

你可能感兴趣的文章
SpringMVC和Mybatis整合使用的配置文件
查看>>
代码特效插件pycharm
查看>>
python实现tcp客户端从服务端下载文件
查看>>
将字符串 “k:1|k1:2|k2:3|k3:4” 转换成字典{“k”:1,”k1”:2,”k2”:3,”k3”:4}
查看>>
AttributeError: 'tuple' object has no attribute 'decode'
查看>>
node爬虫(牛刀小试)
查看>>
关于vue的seo优化
查看>>
字符串在html中的页面中的换行
查看>>
react父子组件间的通信和传值
查看>>
vue-cli3.0设置环境变量
查看>>
vue父组件直接操作子组件的方法(不通过$emit和$on)
查看>>
vue上传文件到UCloud
查看>>
获取input选择文件的本地地址
查看>>
React绑定全局方法或变量
查看>>
js监听div标签上面的自定义属性
查看>>
navcat如何重置窗口
查看>>
代码注入
查看>>
off-by-one
查看>>
ctf-pwn的一些小技巧
查看>>
POJ 1915 Knight Moves
查看>>