IT Community - Software Programming, Web Development and Technical Support

Using Dictionary in C#

This is a discussion on Using Dictionary in C# within the C# Programming forums, part of the Software Development category; Hi, I want to give a brief explanation about Dictionary class in Generic namespace. The Dictionary generic class provides a ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-13-2007, 01:47 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Using Dictionary in C#

Hi,

I want to give a brief explanation about Dictionary class in Generic namespace.

The Dictionary generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary class is implemented as a hash table.

As long as an object is used as a key in the Dictionary, it must not change in any way that affects its hash value. Every key in a Dictionary must be unique according to the dictionary's equality comparer. A key cannot be a null reference (Nothing in Visual Basic), but a value can be, if the value type TValue is a reference type.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-13-2007, 01:51 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Using Dictionary in C#

Syntax:

Dictionary<key, value> sampleDictionary = new Dictionary<key, value>;

As elements are added to a Dictionary, the capacity is automatically increased as required by reallocating the internal array.

Type Parameters

TKey

The type of the keys in the dictionary.

TValue

The type of the values in the dictionary.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-13-2007, 01:54 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: Using Dictionary in C#

EX:

Dictionary<string, string> dictionary =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
dictionary .Add("txt", "notepad.exe");
dictionary .Add("bmp", "paint.exe");
dictionary .Add("dib", "paint.exe");
dictionary .Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
dictionary .Add("txt", "winword.exe");
}
catch (ArgumentException)
{
MessageBox.Show("An element with Key = " +txt+ " already exists.");
}

If you try to add a key value as duplicate, an exception is raised.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-24-2007, 02:00 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 882
kingmaker is on a distinguished road
Send a message via Yahoo to kingmaker
Default Re: Using Dictionary in C#

hi ,

The type System.Collections.Hashtable is not supported because it implements
IDictionary.


did u used the Hashtable in Webservice ...it causes error..Did u know....about that...
__________________
The KINGMAKER
Makes Every Thing Possible

Stuffs (My Blog)

Last edited by kingmaker : 12-24-2007 at 02:07 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-24-2007, 02:18 AM
Anandavinayagam Anandavinayagam is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 131
Anandavinayagam is on a distinguished road
Default Re: Using Dictionary in C#

Hey,


Pls Check the Supported datatypes

Welcome to the MSDN Library
__________________
The MOSS
Master of Solution Service
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the value in the dictionary both Key and Value collections simultaneously? oxygen C# Programming 1 07-26-2007 08:00 AM
data dictionary views sivakumar Database Support 1 07-18-2007 10:42 PM
What is the Dictionary class vadivelanvaidyanathan Java Programming 1 07-17-2007 05:59 AM


All times are GMT -7. The time now is 11:59 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0