Posts Tagged ‘value’

Enumerate Through Dictionary C#

Continuing from a previous post on Using C# Dictionary Collection, the next most common tasks is enumerate through every element in the Dictionary. To begin, each element in a Dictionary is a KeyValuePair. According to the C# documentation for Dictionary, the order of a KeyValuePair enumeration is not defined. Therefore, the KeyValuePair order may not be the same as the insertion (or Add) order. For that reason, we cannot use a for loop. Instead, a foreach loop is our solution. In this tutorial, I will demonstrate how to loop through the KeyValuePair, the Key, and the Value of a dictionary.

Foreach KeyValuePair

In this first example, we will be looping through the KeyValuePair. We can get both the key (as an int) and the value (as a string) from the KeyValuePair. Note that the type of the KeyValuePair must be declared in the foreach loop.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (KeyValuePair pair in myDictionary)
{
    int key = pair.Key;
    string value = pair.Value;
    Debug.WriteLine("Key: " + key.ToString() + " Value: " + value);
}

Foreach Key

In this second case, we will be looping through all Keys in the Dictionary. The Keys must be unique and in this example, the key is an integer.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (int key in myDictionary.Keys)
{
    Debug.WriteLine("Key: " + key.ToString());
}

Foreach Value

In this final case, we are looping through the Value of our dictionary. The value can be anything you define, but in our example, it is a string.

Dictionary myDictionary = new Dictionary();
myDictionary.Add(1, "ABC");
myDictionary.Add(2, "DEF");

foreach (string value in myDictionary.Values)
{
    Debug.WriteLine("Value: " + value);
}

Using C# Dictionary Collection

A Dictionary object in C# has become one of my recent favorites. Below are a few important aspects:

  1. Requires including System.Collections.Generic;
  2. Each entry in a Dictionary is a KeyValuePair (consisting of two objects: key and value)
  3. The key must be unique
  4. The value does not need to be unique
  5. Both the key and value can be any object (string, int, custom class, etc…)
  6. Retrieving a value via a key take close to O(1) time
  7. The order of a KeyValuePair enumeration is not defined

Next is an example of how to create and initialize a Dictionary object. Our Dictionary will have an integer and as a key and a string as a value.

Dictionary<int, string> myDictionary = new Dictionary<int, string>();

Now that we have our Dictionary datatype, we will add two values to the Dictionary. Note that at this point, intellisense knows the two expected types.

myDictionary.Add(1, "Victor");
myDictionary.Add(2, "Billy");

Finally, deleting an object is just as easy:

myDictionary.Remove(1);

And these are the basics to using a Dictionary collection!

Store Constant Variables in WebConfig

In a web application, there is sometimes a need to store a read only string accessible throughout the project without having to access a database. An easy way to achieve this end goal is to create a key in the web.config file.

An advantage to adding a key to the appSettings of the web.config file is that the key will be accessible to all files in your project. Secondly, if this key should ever change, you can edit the key’s value without the need to recompile! Therefore, the value is semi-constant in that it doesn’t change during execution, but can be easily altered between builds. The key is added within appSettings of configuration in the web.config file.

<configuration>
<appSettings>
</appSettings>
<configuration>

Next is a simple example of how this can be used. Below is the relevant code sample of the web.config file.

<configuration>
<appSettings>
<add key =”supportEmailGroup” value=”support@victorchen.info”/>
</appSettings>
<configuration>

To access the key named supportEmailGroup, use the following code below. In the example below, the supportEmailGroup value is accessed in the code behind.

string supportEmailAddress = ConfigurationSettings.AppSettings["supportEmailGroup"];

It is that easy. The web.config file can hold an arbitrary number of keys. I use the appSettings feature in every project. Some example of where this can be useful is pointing to file directory or connection strings.