ToDictionary मेथड C# में एक एक्सटेंशन मेथड है और संग्रह को Dictionary में बदलता है।
सबसे पहले, एक स्ट्रिंग सरणी बनाएं:
string[] str = new string[] {"Car", "Bus", "Bicycle"};अब, संग्रह को Dictionary में बदलने के लिए Dictionary मेथड का उपयोग करें:
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"}; // 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