.Net, ASP.NET, Collections, Programming, VB.NET, WebForms

The ASP.NET Dictionary Object

I have been working with ASP.NET collections and thought a more detailed look into one of the most useful classes also know as the Dictionary collection object is in order.

To review: the Dictionary class is part of the System.Collections.Generic Namespace. If you haven’t already, then it’s worth having a look at the list of collections contained in the namespace to become familiar with the various collections options available to you. The main benefit of the using collections from the Generic namespace is that the collections are strongly typed, giving you better performance and type safety.

The Dictionary collection stores key – value pairs much like a Hashtable from the System.Collections Namespace.  However where a Hashtable has an inbuilt re-ordering system based on your provided keys, the Dictionary collection works on a First-In-First-Out (FIFO) basis. This can be a real boon if you want key value pairs that don’t get randomly re-arranged. The Dictionary object is not documented to be FIFO, but it has been working this way since .NET 1.1 so I hope that this will continue to be the case. Similar objects to the Dictionary class are the SortedList and the SortedDictionary classes, but these do have inbuilt sorting similar to the Hashtable’s sorting, so they are only situationally useful.

In addition to the Dictionary object, I like to use ArrayLists and string arrayx. So as part of my example code I show how to store a  String array inside a Dictionary object as the value. This can be quite useful in a number of ways depending on your goals, but the flexibility this method offers is something to keep in mind.

If you are interested in reading the API for the Dictionary object then I definitely recommend reading the official Microsoft Documentation. An interesting point that’s covered by the  documentation is that using TryGetValue is the most efficient syntax to use rather than ContainsKey or ContainsValue.

The Example Code and Output

I am including below a full example page in VB.NET of putting the Dictionary object through its various exercises.

Notable examples in the code I am providing include looping through the Dictionary items using the KeyValuePair class, storing an array of strings as the value, using TryGetValue to return a value, and checking for the existence of data using ContainsKey and ContainsValue.

The output values are just example text that I input off the top of my head. The actual output of the page will be as follows:


Label : zed – zebra : bed – branch : add – apple : xod – xtra
True *yes contains xod*
False *not contains fud*
True *yes contains branch*
False *not contains bios*

five *retrieving an array value of ‘five’ for the second key


<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub form1_Load(sender As Object, e As System.EventArgs)
 ' Example Construction of a pure string key/value collection
 Dim abc As New Dictionary(Of String, String)
 abc.Add("zed", "zebra")
 abc.Add("bed", "branch")
 abc.Add("add", "apple")
 abc.Add("xod", "xtra")

 ' Loop through key value pair
 For Each item As KeyValuePair(Of String, String) In abc
 Label1.Text += " : " & item.Key & " - " & item.Value

 ' positive contains key 
 Label2.Text = abc.ContainsKey("xod")

 'negative contains key 
 Label3.Text = abc.ContainsKey("fud")

 ' positive contains value
 Label4.Text = abc.ContainsValue("branch")

 'negative contains value 
 Label5.Text = abc.ContainsValue("bios")

 ' Example Construction of a string key and string array value collection
 Dim strArrayOne As String() = {"one", "two", "three"}
 Dim strArrayTwo As String() = {"four", "five", "six"}
 Dim def As New Dictionary(Of String, String())
 def.Add("zed", strArrayOne)
 def.Add("apple", strArrayTwo)

 ' Example of using TryGetValue to retrieve a stored array value
 Dim arrValues As String() 
 If def.TryGetValue("apple", arrValues) Then
 Label6.Text = arrValues(1)
 End If

 End Sub
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <form id="form1" runat="server" onload="form1_Load">
 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
 <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> *yes contains xod*<br />
 <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> *not contains fud*<br />
 <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> *yes contains branch*<br />
 <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label> *not contains bios*<br /><br />
 <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label> *retrieving an array value of 'five' for the second key<br />

2 thoughts on “The ASP.NET Dictionary Object”

  1. These objectives contain code execution surroundings that lessens software package deployment, code atmosphere that guarantees risk-free execution of code and much more.

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 )

Facebook photo

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

Connecting to %s