Dictionary

A Dictionary class represents a dictionary in C# that is used to represent a collection of keys and values pair of data.

1.Dictionary and hashtable
The Hashtable class is a specific type of dictionary class that uses an integer value (called a hash) to aid in the storage of its keys. The Hashtable class uses the hash to speed up the searching for a specific key in the collection. Every object in .NET derives from the Object class. This class supports the GetHash method, which returns an integer that uniquely identifies the object. The Hashtable class is a very efficient collection in general. The only issue with the Hashtable class is that it requires a bit of overhead, and for small collections (fewer than ten elements) the overhead can impede performance. The HashTable is the base class that is weakly type; the DictionaryBase abstract class is stronly typed and uses internally a HashTable.

There is one more important difference between a HashTable and Dictionary. If you use indexers to get a value out of a HashTable, the HashTable will successfully return null for a non-existent item, whereas the Dictionary will throw an exception if you try accessing a item using a indexer which does not exist in the Dictionary

2. Dictionary and List
Dictionary lookup performance is greater than list after the elements exceeded 5. If the operation is only related to lookup, the best choice is Dictionary.


3. Using Dictionary


    Private _DicArrow As New Dictionary(Of String, Integer)
    If _DicArrow.ContainsKey(sender.ID) Then
       _DicArrow(sender.ID) = arrowtype
    Else
      _DicArrow.Add(sender.ID, arrowtype)
    End If
    If _DicArrow.ContainsKey(lb.ID) Then
      If _DicArrow(lb.ID) = 2 Then
          lb.Text = lb.Text + " " + "↑"
       Else
          lb.Text = lb.Text + " " + "↓"
       End If

     End If
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s