Home Page
Category: Arrays and Lists

Click here to download the sample source code for this Dictionary lab exercise 

There is another useful C# class that I would like to cover as part of the Arrays and Lists lessons.  This class is called the .  The Dictionary is also part of the System.Collections.Generic namespace but it has a different purpose than a List object.  The Dictionary object is useful when you need to store key/value pairs in memory.  For example if you want to store attributes about a web application user such as ip address, first name, last name, userid you could setup a Dictionary as such:

Key          Value
—-          ——
ip              129.168.29.38
fname      Billy
lname      Gibbons
uid           bgibbons

You could access the last name of the user by using the syntax mydictionary[”lname”].  In a Dictionary, the key is always unique.  Let’s do an example together.  Create a new web form Default3.aspx and put a Button and a TextBox on the form.  Change the Text of the Button to “Dictionary”.  Double click the Button to create the event handler and put:

 

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

mydictionary["ted"] = "kolovos";
mydictionary["mark"] = "jenkins";

TextBox1.Text = "The item at position ted is: " +
    mydictionary["ted"];

Make sure you have System.Collections.Generic in your namespaces at the top of the code.  In this example above, notice that inside the < > following the Dictionary type declaration I had to specify 2 things.  The first is the data type for the key of the Dictionary which is string.  The second is the data type for the value of the Dictionary which happens to also be string.  The data type for the key and value can be anything you want.

I put an item into the Dictionary using the syntax mydictionary[”ted”] and the assignment operator.  Then when I want to later determine what is stored inside the Dictionary with the key of “ted” I use the exact same syntax with the square brackets.  Remember that the key in a Dictionary is always unique.  Also note that if you are using strings as the Dictionary key, it is case sensitive, so mydictionary[”ted”] is a different place in the Dictionary versus mydictionary[”Ted”].  Because of this sometimes programmers change the key to upper case before loading data into the Dictionary and when accessing the contents to avoid invalid searches.

The Dictionary object is idea when you want to store key/value pairs or perform searches based on a key value.  If you simply want to store a list of objects and are not looking for any search capability, use the List object instead.

The Dictionary object has a method called ContainsKey which can search the Dictionary to determine if an item exists.  Here is an example that shows how it can be used:

 

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

mydictionary["ted"] = "kolovos";
mydictionary["mark"] = "jenkins";

if (mydictionary.ContainsKey(txtFirstname.Text))
    txtOutput.Text = mydictionary[txtFirstname.Text];
else
    txtOutput.Text = "That doesn't exist";

Refer to the Default3.aspx file sample source code provided as part of Lab3_csharpuniversity.zip to see the web form for this example.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment