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
Pair Class in C#
The KeyValuePair structure in C# allows you to store a pair of related values as a single unit. It is commonly used in collections like Dictionary and can be used in lists to store paired data such as name-value combinations or key-identifier pairs.
The KeyValuePair<TKey, TValue> is a generic structure that provides a way to store two related pieces of data together, where TKey represents the key type and TValue represents the value type.
Syntax
Following is the syntax for creating a KeyValuePair −
KeyValuePair<TKey, TValue> pair = new KeyValuePair<TKey, TValue>(key, value);
Following is the syntax for creating a list of KeyValuePair −
var list = new List<KeyValuePair<TKey, TValue>>(); list.Add(new KeyValuePair<TKey, TValue>(key, value));
Properties
The KeyValuePair structure provides two main properties −
Key − Gets the key in the key/value pair.
Value − Gets the value in the key/value pair.
Creating and Using KeyValuePair
Example
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", 1));
myList.Add(new KeyValuePair<string, int>("Desktop System", 2));
myList.Add(new KeyValuePair<string, int>("Tablet", 3));
myList.Add(new KeyValuePair<string, int>("Mobile", 4));
myList.Add(new KeyValuePair<string, int>("E-Book Reader", 5));
myList.Add(new KeyValuePair<string, int>("LED", 6));
foreach (var val in myList) {
Console.WriteLine(val);
}
}
}
The output of the above code is −
[Laptop, 1] [Desktop System, 2] [Tablet, 3] [Mobile, 4] [E-Book Reader, 5] [LED, 6]
Accessing Key and Value Properties
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var products = new List<KeyValuePair<string, double>>();
products.Add(new KeyValuePair<string, double>("Laptop", 999.99));
products.Add(new KeyValuePair<string, double>("Mouse", 25.50));
products.Add(new KeyValuePair<string, double>("Keyboard", 75.00));
Console.WriteLine("Product Details:");
foreach (var product in products) {
Console.WriteLine($"Product: {product.Key}, Price: ${product.Value}");
}
// Accessing individual elements
Console.WriteLine($"\nFirst product: {products[0].Key} costs ${products[0].Value}");
}
}
The output of the above code is −
Product Details: Product: Laptop, Price: $999.99 Product: Mouse, Price: $25.5 Product: Keyboard, Price: $75 First product: Laptop costs $999.99
Creating KeyValuePair with Different Data Types
Example
using System;
using System.Collections.Generic;
class Program {
static void Main() {
// Different data type combinations
var studentGrades = new List<KeyValuePair<string, char>>();
var settings = new List<KeyValuePair<string, bool>>();
studentGrades.Add(new KeyValuePair<string, char>("John", 'A'));
studentGrades.Add(new KeyValuePair<string, char>("Mary", 'B'));
settings.Add(new KeyValuePair<string, bool>("DarkMode", true));
settings.Add(new KeyValuePair<string, bool>("AutoSave", false));
Console.WriteLine("Student Grades:");
foreach (var grade in studentGrades) {
Console.WriteLine($"{grade.Key}: {grade.Value}");
}
Console.WriteLine("\nSettings:");
foreach (var setting in settings) {
Console.WriteLine($"{setting.Key}: {setting.Value}");
}
}
}
The output of the above code is −
Student Grades: John: A Mary: B Settings: DarkMode: True AutoSave: False
Conclusion
KeyValuePair in C# is a versatile structure for storing two related values as a single unit. It's particularly useful when you need to maintain associations between different data types and provides easy access to both key and value components through its properties.
