Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Dictionary object in ASP.NET

Author: Richard Wand

This article will give you a good detail about Dictionary object, its creation and usage in ASP.NET. It will also explain about difference between Dictionary and Hashtable and Arrays.

 

 
What is Dictionary in ASP.NET?
 
The Dictionary is a collection of Key/Value pairs. It is actually a generic collection that works with Key/Value pair and we declare type for Key and Value. We can declare any type of Key and Value like string, number or any other data type. A Dictionary is a lookup container where we values are accessed using keys. Dictionary is useful when you want to store value with defined key and later you want to look up your values using key. Each value in the dictionary is associated with a key. A Key can be of any data type but it must be unique within a dictionary and one key cannot be used for another value in the dictionary. In ASP.NET Dictionary class represents dictionary that is used to store data in a key/value pair.
 
Difference between Dictionary and HashTable
 
HashTable is actually a basic type of Dictionary following differences.
 
  • Dictionary is generic collection while HashTable is non-generic collection.
  • Dictionary stores key/value pair in any data type while HashTable stores key/value pair in a form of object.
  • We get type safety with Dictionary while HashTable is not type safe.
  • So we have to face the additional cost of boxing and unboxing in HashTable
  • Hashtable supports multiple reader thread with single writer thread while Dictionary doesn’t provide any thread safety.
 
Difference between Dictionary and Arrays  
 
Both Dictionary and Array are generic collection but they have some difference as well.
 
  • Dictionary is an unordered collection while array is the ordered collection.
  • Dictionary is a look up container while array is not a look up container
  • Dictionary finds value with key so it requires less time to find an item in dictionary than array.
  • Array can be multidimensional while dictionary cannot be multidimensional.

 

How to create Dictionary object in ASP.NET

We can create Dictionary object in ASP.NET as below.

C#

Dictionary<string, string> myDictionary = new Dictionary();
 
VB.NET

Dim myDictionary As New Dictionary(Of String, String)()
 
How to add items to Dictionary objects
 
Items can be added to Dictionary object as below.
 
C#
 
myDictionary.Add("Key1", "First Value");
myDictionary.Add("Key2", "Second Value");
 
VB.NET
 
myDictionary.Add("Key1", "First Value")
myDictionary.Add("Key2", "Second Value")
 
How to retrieve items from Dictionary
 
Items from dictionary can be retrieved in the following way.
 
C#
 
foreach (KeyValuePair pair in myDictionary)
{
       Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value);
}
 
VB.NET
 
For Each pair As KeyValuePair(Of String, String) In myDictionary
       Console.WriteLine("Key = {0}, Value = {1}", pair.Key, pair.Value)
Next

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to display image in a new window by clicking on the image in ASP.NET

Disclaimer - Privacy
© 2002-2014 DevASP.net