Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
KeyValuePair in C#
The KeyValuePair struct in C# stores a pair of values in a single object, where one value acts as the key and the other as the value. It is commonly used in collections like List<KeyValuePair> and as the element type in dictionaries.
KeyValuePair is particularly useful when you need to associate two related pieces of data together without creating a custom class or when working with dictionary enumerations.
Syntax
Following is the syntax for creating a KeyValuePair −
KeyValuePair<TKey, TValue> pair = new KeyValuePair<TKey, TValue>(key, value);
Following is the syntax for accessing key and value −
TKey key = pair.Key; TValue value = pair.Value;
Using KeyValuePair with List
You can store multiple key-value pairs in a List collection −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var myList = new List<KeyValuePair<string, int>>();
// adding elements
myList.Add(new KeyValuePair<string, int>("Laptop", 20));
myList.Add(new KeyValuePair<string, int>("Desktop", 40));
myList.Add(new KeyValuePair<string, int>("Tablet", 60));
foreach (var val in myList) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
[Laptop, 20] [Desktop, 40] [Tablet, 60]
Accessing Key and Value Separately
You can access the Key and Value properties individually −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var inventory = new List<KeyValuePair<string, int>>();
inventory.Add(new KeyValuePair<string, int>("Books", 150));
inventory.Add(new KeyValuePair<string, int>("Pens", 300));
inventory.Add(new KeyValuePair<string, int>("Notebooks", 75));
foreach (var item in inventory) {
Console.WriteLine("Product: " + item.Key + ", Quantity: " + item.Value);
}
}
}
The output of the above code is −
Product: Books, Quantity: 150 Product: Pens, Quantity: 300 Product: Notebooks, Quantity: 75
KeyValuePair with Dictionary
KeyValuePair is the underlying element type when iterating through Dictionary collections −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, double> prices = new Dictionary<string, double>();
prices.Add("Coffee", 4.99);
prices.Add("Tea", 3.50);
prices.Add("Juice", 2.75);
foreach (KeyValuePair<string, double> kvp in prices) {
Console.WriteLine("Item: " + kvp.Key + ", Price: $" + kvp.Value);
}
}
}
The output of the above code is −
Item: Coffee, Price: $4.99 Item: Tea, Price: $3.5 Item: Juice, Price: $2.75
KeyValuePair vs Dictionary
| KeyValuePair | Dictionary |
|---|---|
| Stores a single key-value pair | Stores multiple key-value pairs with fast lookup |
| Immutable once created | Mutable - can add, remove, update entries |
| No built-in key uniqueness enforcement | Enforces unique keys automatically |
| Used as element type in collections | Optimized hash-based collection |
Conclusion
KeyValuePair in C# is a lightweight structure for storing paired data values. It's commonly used in collections when you need to associate two pieces of related information, and serves as the element type when iterating through dictionaries.
