KeyValuePair<TKey, TValue> and IDictionary<TKey, TValue> in C# have unique purposes and exhibit distinct characteristics.
Features of KeyValuePair<TKey, TValue>
A KeyValuePair<TKey, TValue> is a data structure that stores a single key-value pair. It belongs to the System.Collections.Generic namespace.
Usage
It is utilized to represent individual key-value pairs, typically in the context of enumerations or when multiple values need to be returned from a method.
Frequently employed with collections that implement key-value pairs, like dictionaries, but it can also be used independently.
Immutability
After creation, the key and value of a KeyValuePair cannot be modified as its properties are read-only (DotNetPerls).
Properties
- Key: Gets the key in the key-value pair.
- Value: Gets the value in the key-value pair.
Note. It is possible for KeyValuePair<TKey, TValue> to contain duplicate keys.
Example
using System.Windows;
using System.Collections.Generic;
using System;
namespace KeyValuePairDictionaryExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
GenerateKeyValuePairList();
}
private void GenerateKeyValuePairList()
{
// Generate a collection of KeyValuePair<string, string>.
List<KeyValuePair<string, string>> keyValuePairsList = new List<KeyValuePair<string, string>>();
// Populate the list with initial key-value pairs.
keyValuePairsList.Add(new KeyValuePair<string, string>("firstKey", "First Value"));
keyValuePairsList.Add(new KeyValuePair<string, string>("secondKey", "Second Value"));
keyValuePairsList.Add(new KeyValuePair<string, string>("thirdKey", "Third Value"));
keyValuePairsList.Add(new KeyValuePair<string, string>("thirdKey", "Fourth Value"));
keyValuePairsList.Add(new KeyValuePair<string, string>("thirdKey", "Fifth Value"));
keyValuePairsList.Add(new KeyValuePair<string, string>("thirdKey", "Sixth Value"));
// Create a separate list to hold items to add
List<KeyValuePair<string, string>> itemsToAddToExistingList = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("fourthKey", "Seventh Value"),
new KeyValuePair<string, string>("fifthKey", "Eighth Value")
};
// Add new items outside of the enumeration
foreach (var kvp in itemsToAddToExistingList)
{
keyValuePairsList.Add(kvp);
}
ShowKeyValuePairs(keyValuePairsList);
}
private void ShowKeyValuePairs(List<KeyValuePair<string, string>> keyValuePairsList)
{
foreach (KeyValuePair<string, string> kvp in keyValuePairsList)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
}
}
Features of Dictionary<TKey, TValue>
The Dictionary<TKey, TValue> class is responsible for managing a set of key-value pairs. It is a component of the System.Collections.Generic namespace.
Purpose
The main purpose of this class is to store multiple key-value pairs, ensuring that each key is unique. It offers efficient operations for retrieving, adding, and deleting these pairs.
Mutability
In terms of mutability, you have the ability to add, remove, and update elements within a Dictionary.
Usage
This class is commonly used when you need to manage a collection of data, where each element is identified by a unique key.
Example
using System;
using System.Windows;
using System.Collections.Generic;
namespace KeyValuePairDisctionaryExample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
GenerateDictionary();
}
private void GenerateDictionary()
{
Dictionary<string, string> dictCollection = new Dictionary<string, string>
{
{"firstKey", "First Value"},
{"secondKey", "Second value"},
{"ThirdKey", "Third Value"},
{"FourthKey", "Fourth Value"}
};
dictCollection.Add("FifthKey", "FifthValue");
if (dictCollection.ContainsKey("secondKey"))
{
Console.WriteLine(dictCollection["secondKey"]); // Outputs: Second value
}
foreach (var kvp in dictCollection)
{
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
}
}
Note. In a dictionary, it is not permissible to have duplicate keys.