ToDictionary विधि C# में एक विस्तार विधि है और संग्रह को शब्दकोश में परिवर्तित करती है।
सबसे पहले, एक स्ट्रिंग ऐरे बनाएं -
string[] str = new string[] {"Car", "Bus", "Bicycle"}; अब, किसी संग्रह को डिक्शनरी में बदलने के लिए डिक्शनरी विधि का उपयोग करें -
str.ToDictionary(item => item, item => true);
यहाँ पूरा कोड है -
उदाहरण
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
string[] str = new string[] {"Car", "Bus", "Bicycle"};
// key and value under ToDictionary
var d = str.ToDictionary(item => item, item => true);
foreach (var ele in d) {
Console.WriteLine("{0}, {1}", ele.Key, ele.Value);
}
}
} आउटपुट
Car, True Bus, True Bicycle, True