Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

12,753,418
Sign up formembers ﴾35,203
our free online﴿
weekly Web Developer Newsletter. Sign in
×

Search for articles, questions, tips

home articles quick answers discussions features community help

List vs IEnumerable vs IQueryable vs ICollection vs IDictionary


Mahsa Hassankashi, 23 Feb 2015 CPOL Rate this:
   4.88 ﴾304 votes﴿

This article is one of the first sources ﴾with complete and clear comparision﴿ about data structure, Array, ArrayList, List, IList, ICollection, Stack, Queue, HashTable,
Dictionary, IQueryable, IEnumerable.

IList vs IEnumerable vs IQueryable vs ICollection vs IDictionary ‐ 494.3 KB

Demo For Data Access Layer

https://www.codeproject.com/KB/cs/832189/CompleteCollectionComparison.jpg

Collection
Collection is set of related records. It includes a meaningful unit: 
We should select appropriate container to store data  temporarily for fetch and modification process. 
Appropriate container depends on:
1. Our aim which we want to do on data ﴾ just reading, doing modification such as insert, delete, update ﴿ 
2. Number of records which should be transferred

Array
1. Fixed Length ‐> Its size is not flexible. It is determined at instantiation time.

 2. Strongly Typed ‐> Developers determine its type at the instantiation time instead of runtime. This feature makes to run fast at runtime, since it does not need to wait
for type definition. 
 
 3. Developers use "foreach" keyword to fill and iterate through array.

Fixed Length and Strongly Typed consume less memory, therefore it has good performance.

Hide   Copy Code

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 1/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

//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(); 

ArrayList
1. Arraylist is NOT Fixed Length ‐> It is possible that data grows. On the one hand it is a good feature whenever developers are not sure about size of arraylist and on
the other hand it might take long time for size definition.

2. Arraylist is NOT Strongly Typed ‐> Whenever developers are not sure about what is exactly type definition for input or output data and they should wait until runtime
to appear its type. Its disadvantage is time consuming at the runtime for memory to determine type definition.

3. Developers use "foreach" keyword to fill and iterate through arraylist.


 

Hide   Copy Code


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 can accept string, integer and decimal Simultaneously.

Hide   Shrink   Copy Code

 //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 defered 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(); 

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 2/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

 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
HashTable is another kind of data structure that defines key value for each data section. Therefore finding data is easy just by point out to its key. It is NOT strongly
typed and NOT fixed size.

Hide   Copy Code


//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

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 3/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
We have different data structure and stack is one of them. Stack is subset of data structure. Stack is prioritized data structure ﴾such as List is indexed base﴿. Stack defines
priority for each item, it means stack behavior force its items to put ﴾push﴿ inside stack prioritized form. So stack put later item on the top of items and this behavior is
"defining priority for each item". Therefore whenever you want to insert item, you should add ﴾PUSH﴿ it at the top of the stack and whenever you want to remove ﴾POP﴿
item from stack you should remove it from top of the stack. As you got it, item that comes last one will be selected to POP for first one and its expression in computer
science is equal to "Last in First out" == "LIFO".

It is NOT strongly typed and NOT fixed size.

Stack ‐ Push

Hide   Copy Code


//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(); 

Stack ‐ Pop

Hide   Copy Code


  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
Queue is another kind of data structure that defines priority for each item in other form. Therefore whenever you want to insert item, you
should add ﴾Enqueue﴿ it at the head of the queue and whenever you want to remove ﴾Dequeue﴿ item from queue you should remove it
from bottom of the queue. As you got it, item that comes first one will be selected to Dequeue for first one and its expression in computer
science is equal to "First in First out" == "FIFO".

It is NOT strongly typed and NOT fixed size.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 4/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Queue ‐ Enqueue

Hide   Copy Code


 //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(); 

Queue ‐ Dequeue

Hide   Copy Code


  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(); 

List
Why we need List?

1. List is NOT Fixed Length ‐> It is possible that data grows. On the one hand it is a good feature whenever developers are not sure about size of arraylist and on the
other hand it might take long time for size definition.

2. List is Strongly Typed when it is defined "Generic" ‐> Whenever developers are sure about what is exactly type definition for input or output data and they do not
wait until runtime to appear its type. This feature makes to run fast at runtime, since it does not need to wait for type definition. 

3. Developers use "foreach" keyword to fill and iterate through array.

Since List is not Fixed Length makes developers feel flexible to use it, and because of it is Strongly Typed when it is defined "Generic" so our
code runs fast at runtime because it does not need to wait for type definition.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 5/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Hide   Copy Code


//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
Why we need IList? IList is implemented by List, Ilist is an interface and implements methods. Whenever you estimate probability that your code would be changed in
future you have to use IList because interface reduces dependency and with the little modification your code runs. Therefore you should observe polymorphism in order
to decouple your app and control on adding or removing method that might be changed. Everything else is similar. Whenever we want to change on some operation,
so “IList” allow us to do that easily with at least changing in the whole of codes. 

Interfaces can not be instantiate, so it should be instantiate from List

Hide   Copy Code


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

Difference between Concrete Class and Interface


1. Concrete Class inherits from just ONE class but it Can implements one or MORE than one interfaces

2. You can write inside concrete class full version of your function while you have to define just signature inside the interface.

3. You can define variable and value Inside Concrete Class while you are not allowed to define variable inside interfaces.

4. An Concrete class can have constructor while you are not allowed to define constructor inside interfaces.

5. Abstract class can containe access modifier while intefaces does not.

As I mentioned that how a class cannot be driven from two class it just can be driven from only one class, so whenever you want to drive from two class it is not
possible to inherit from two abstract class but it is possible to drive from more than one class by interfaces.

Later in future if developers decide to add some features on their class and inherit it from another class, developers always prefer to use interface of collection so that if
you want to change your code and enhance its abilities, choose interfaces.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 6/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
On the other hand interface keeps your program extensible and Decouple: The classes are independent from each other, so error, exception and failure will happen
rarely in future changing code by interfaces.

Polymorphism: When you are using interface you absolutely observe and do polymorphism and oop. It means encapsulate your designer. Interface means a point or a
node to join two part to each other it means making low dependency from two part and make a joint section to make flexible change in future.

Hide   Copy Code


//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 is suitable just for iterate through collection and you can not modify ﴾Add or Remove﴿ data IEnumerable bring ALL data from server to client then filter
them, assume that you have a lot of records so IEnumerable puts overhead on your memory.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 7/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Hide   Copy Code


//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
Whenever we encounter to huge data with so many records so we have to reduce overhead from application. IQueryable prepares high performance in such situations
﴾huge data﴿ by filtering data firstly and then sending filtered data to client.

Follow "Entity Framework DatabaseFirst" from this article for making DBContext

Hide   Copy Code


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

Data Access

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 8/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Hide   Shrink   Copy Code

//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 cluse 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 can not 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(); 
</employee>

SQL Profiler:

How to trace your query generates TSQL & How many records will be loaded:

Step 1:
Start ‐> MS SQL Server 2008 ‐> Performance Tools ‐> SQL Server Profiler

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 9/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

 
 

Step 2:

SQL Server Profiler ‐> File ‐> New Trace

Step 3:

Connect with you user name and password.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 10/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

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" 

Press Run

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 11/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Step 6:

Go To MS SQL Server Management Studio ‐> Count all of records ﴾records=5﴿

Step 7:

IEnumerable generates:

Hide   Copy Code


SELECT  
[Extent1].[ID] AS [ID],  
[Extent1].[Name] AS [Name],  
[Extent1].[Age] AS [Age] 
FROM [dbo].[Employee] AS [Extent1] 

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 12/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

IQueryable generates:

Hide   Copy Code


SELECT  
[Extent1].[ID] AS [ID],  
[Extent1].[Name] AS [Name],  
[Extent1].[Age] AS [Age] 
FROM [dbo].[Employee] AS [Extent1] 
WHERE 1 = [Extent1].[ID] 

ICollection
ICollection inherits from IEnumerable. There is one difference:

you can find IEnumerable[ i ] ‐‐> Index Based

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 13/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
you can NOT find ICollection[ i ] ‐‐> Not Index Based

https://www.codeproject.com/KB/cs/832189/ICollection.jpg

Hide   Shrink   Copy Code

  //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; 

Stack Generic

Push:
Hide   Copy Code
//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:
Hide   Copy Code
//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();

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 14/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
 

Queue Generic
Enqueue:
Hide   Copy Code
//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();

Dequeue:
Hide   Copy Code
//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();

Dictionary and IDictionary


Dictionary is GENERIC while HashTable is not generic : Dictionary<TKey, TValue>, There is a tiny difference between them that if dictionary can not find specific key will
throw an exception while HashTable just returns null.

IDictionary is interface and if you estimate big changes in future use IDictionary instead of Dictionary.

Hide   Copy Code


//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,>

MVC Tutorial: http://technical.cosmicverse.info/Article/Index/3

Data Access Layer Tutorial: http://technical.cosmicverse.info/Article/Index/4

References

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 15/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
msdn.microsoft.com: Commonly Used Collection Types

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/

Feedback
Feel free to leave any feedback on this article; it is a pleasure to see your comments and vote about this code. If you have any questions, please do not hesitate to ask
me here.

License
This article, along with any associated source code and files, is licensed under The Code Project Open License ﴾CPOL﴿

Share
EMAIL TWITTER

About the Author


Mahsa Hassankashi
Software Developer
Germany

I have been working with .Net framework for 10 years.


I`d like to challenge with complex problem, then make it easy for using everyone. This is the best joy.

ICT Master
Founder at technical.cosmicverse.info ﴾Free Web Tutorial﴿
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Diamond is nothing except the pieces of the coal which have continued their activities finally they have become Diamond.

*Article of The Community Spotlight in Microsoft ASP.NET, Wednesday, February 11, 2015, www.asp.net
*Article of The Day in Microsoft ASP.NET Tuesday, February 3, 2015, www.asp.net/community/articles
*1 Jan 2015: CodeProject MVP 2015
*22 Mar 2014: Best Web Dev Article of February 2014 ‐ Second Prize

http://www.technical.cosmicverse.info

You may also be interested in...


IEnumerable Vs IQueryable Generate and add keyword variations using AdWords API

IEnumerable vs IQueryable Window Tabs (WndTabs) Add-In for DevStudio

SAPrefs - Netscape-like Preferences Dialog WTL for MFC Programmers, Part IX - GDI Classes, Common
Dialogs, and Utility Classes

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 16/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject

Comments and Discussions


 

You must Sign In to use this message board.

Search Comments   Go

First Prev Next

Nice article

nice article

awasome

Another aspect of HashTables

Amazing Aritcle

Re: Amazing Aritcle

Very Nice Article

Re: Very Nice Article

Iqueryable or ienumrable?

Re: Iqueryable or ienumrable?

Re: Iqueryable or ienumrable?

Properly explained

Re: Properly explained

not clear IEnumerable

Re: not clear IEnumerable

Message Removed

Brilliant Article!

Re: Brilliant Article!

My vote of 4

Re: My vote of 4

THANKS FOR THE POST

Re: THANKS FOR THE POST

nice

Re: nice

5 Star Article

Last Visit: 20‐Feb‐17 5:15     Last Update: 22‐Feb‐17 0:23 Refresh 1 2 3 4 5 6 7 8 9 10 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 17/18
22/02/2017 List vs IEnumerable vs IQueryable vs ICollection vs IDictionary ­ CodeProject
Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | Article Copyright 2014 by Mahsa Hassankashi
Select Language ▼
Web01 | 2.8.170217.1 | Last Updated 23 Feb 2015 fluid Everything else Copyright © CodeProject, 1999‐2017

https://www.codeproject.com/Articles/832189/List­vs­IEnumerable­vs­IQueryable­vs­ICollection­v 18/18

You might also like