KeyValuePair - High-Flying C# Coding Tips

Sponsored Links

73058_New Scooba® 230 Floor Washing Robot + Free Shipping!



KeyValuePair is an object which contains 2 objects in a key to value pairing.  The 2 objects can be of any kind of object, so you could have a pair of strings, a pair of integers, or mixing different types you could have an integer and a string pair.  You can even use your own custom objects, so you could have something like a Manufacturer and Make of Car pair.

KeyValuePair is part of Generics in C# (.NET 2.0) so you'll need to call the namespace to use the KeyValuePair object:

using System.Collections.Generic;

To use a KeyValuePair, you create the KeyValuePair and you need to specify which type of objects the KeyValuePair will contain with the syntax KeyValuePair<object, object>.  Once you have created the KeyValuePair, you access the first object which is called the Key, and the second object is called the Value.  When you create the KeyValuePair you set the object values in the constructor.  Once you have created the KeyValuePair, the Key and the Value are read-only so can't be changed.  

KeyValuePair<string, string> Example

In this example we create a KeyValuePair which is made up of 2 strings:

// a KeyValuePair of strings
KeyValuePair<string, string> pairOfStrings = new KeyValuePair<string, string>("Fred", "Smith");

string firstName = pairOfStrings.Key; // ="Fred"
string lastName = pairOfStrings.Value; // ="Smith"

KeyValuePair<int, string> Example

In this example we create a KeyValuePair which is made up of an integer and a string:

// a KeyValuePair of an integer and a string
KeyValuePair<int, string> customerPair = new KeyValuePair<int, string>(123, "Fred Smith");

int ID = customerPair.Key; // =123
string name = customerPair.Value; // ="Fred Smith";

KeyValuePair in a List Example

KeyValuePairs can be put into a List, so they can be stored and organised with the benefits a List brings such as being able to iterate through the List of KeyValuePairs with a ForEach loop and search for a particular KeyValuePair.   For example, you might have a list of employee numbers and their associated employee name:

// List containing KeyValuePairs containing an integer
// and a string
List<KeyValuePair<int, string>> listOfPairs = new List<KeyValuePair<int, string>>();

listOfPairs.Add(new KeyValuePair<int, string>(234, "Fred Smith"));

foreach (KeyValuePair<int, string> pair in listOfPairs)
    if (pair.Key == 234)
        // do something
        string selectedEmployee = pair.Value;

KeyValuePair in a KeyValuePair Example, KeyValuePair<int, KeyValuePair<string, string>>

It's possible to have a KeyValuePair containing a KeyValuePair, so if you needed to have a List of objects which had 3 seperate values per object, you can achieve this by making one of the KeyValuePair values (Key or Value) a KeyValuePair as shown in the following example.  The example shows a KeyValuePair of type integer and a KeyValuePair (which itself contains a string and a string).

// KeyValuePair inside a KeyValuePair
KeyValuePair<int, KeyValuePair<string, string>> triplet =
new KeyValuePair<int, KeyValuePair<string, string>>(123,
new KeyValuePair<string,string>("Ford", "Mustang"));

int identifier = triplet.Key; // = 123

string car = triplet.Value.Key + " " + triplet.Value.Value; // = "Ford Mustang"