In C#, a key-value pair is a fundamental data structure that allows you to associate a unique key with a corresponding value. This data structure is commonly used in various scenarios, such as dictionaries, hash tables, and collections. In this article, we will explore how to create and work with key-value pairs in C#.
What is a Key-Value Pair?
A key-value pair consists of two elements: a key and a value. The key is used to uniquely identify the pair, while the value represents the associated data. This relationship allows for efficient retrieval and manipulation of data based on the key.
How to Create a Key-Value Pair in C
To create a key-value pair in C#, you can use the KeyValuePair<TKey, TValue>
structure. This structure is part of the System.Collections.Generic
namespace and provides a generic implementation for key-value pairs.
Here’s an example of how to create a key-value pair with a string key and an integer value:
KeyValuePair<string, int> pair = new KeyValuePair<string, int>("keyName", 2);
In the above code, we declare a variable pair
of type KeyValuePair<string, int>
. We initialize it with the new
keyword and provide the key and value as arguments to the constructor.
Accessing the Key and Value
Once you have created a key-value pair, you can access its key and value using the Key
and Value
properties, respectively.
string key = pair.Key;
int value = pair.Value;
In the above code, we assign the key and value of the pair
to separate variables key
and value
, respectively.
Modifying the Key and Value
In some cases, you may need to modify the key or value of a key-value pair. However, it’s important to note that the KeyValuePair<TKey, TValue>
structure is immutable, which means you cannot change its properties directly.
To modify the key or value, you need to create a new key-value pair with the updated values. Here’s an example:
pair = new KeyValuePair<string, int>("newKeyName", 5);
In the above code, we create a new key-value pair with a different key and value. By assigning it to the pair
variable, we effectively update the key-value pair.
Using Key-Value Pairs in Collections
One of the most common use cases for key-value pairs is storing them in collections, such as dictionaries or hash tables. These collections provide efficient lookup and retrieval of values based on their keys.
Here’s an example of how to use a dictionary to store and retrieve key-value pairs:
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("key1", 1);
dictionary.Add("key2", 2);
int value1 = dictionary["key1"];
int value2 = dictionary["key2"];
In the above code, we create a dictionary with string keys and integer values. We add two key-value pairs to the dictionary using the Add
method. We can then retrieve the values by specifying the keys in square brackets.
Key-value pairs are a powerful data structure that allows for efficient data retrieval and manipulation. Understanding how to work with them is essential for any C# programmer.