Pair क्लास KeyValuePair क्लास है जो C# में एकल सूची में मानों की एक जोड़ी संग्रहीत करती है।
KeyValuePair घोषित करें:
var myList = new List<KeyValuePair<string, int>>();
// अब, कुछ तत्व जोड़ें:
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));
अब नीचे दिखाए अनुसार KeyValuePair प्रदर्शित करें:
उदाहरण
using System;
using System.Collections.Generic;
class Program {
static void Main() {
var myList = new List<KeyValuePair<string, int>>();
// तत्व जोड़ना
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);
}
}
}
आउटपुट
[Laptop, 1] [Desktop System, 2] [Tablet, 3] [Mobile, 4] [E-Book Reader, 5] [LED, 6]