KeyValuePair वर्ग C# के साथ एकल सूची में मानों की एक जोड़ी संग्रहीत करता है।
KeyValuePair सेट करें और तत्व जोड़ें -
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)); KeyValuePair के साथ काम करने और कुंजियों और मूल्यों को प्रदर्शित करने का तरीका जानने के लिए यहां कोड दिया गया है -
उदाहरण
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);
}
}
}