C# में StringDictionary में कुंजी और मान जोड़ने के लिए, हम Add() मेथड का उपयोग करते हैं। StringDictionary एक विशेष डिक्शनरी है जहां कुंजी और मान दोनों स्ट्रिंग होने चाहिए। यह System.Collections.Specialized नेमस्पेस का हिस्सा है और केस-इनसेंसिटिव कुंजी तुलना का उपयोग करता है (कुंजियों को लोअरकेस में संग्रहीत करता है)।
उदाहरण 1: नामों के साथ StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
strDict.Add("E", "Kevin");
strDict.Add("F", "Katie");
strDict.Add("G", "Brad");
Console.WriteLine("StringDictionary तत्व...");
foreach (DictionaryEntry de in strDict)
{
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
आउटपुट
StringDictionary तत्व... a John b Andy c Tim d Ryan e Kevin f Katie g Brad
उदाहरण 2: वाहन श्रेणियों के साथ StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "Electric Cars");
strDict.Add("2", "SUV");
strDict.Add("3", "AUV");
Console.WriteLine("StringDictionary तत्व...");
foreach (DictionaryEntry de in strDict)
{
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
आउटपुट
StringDictionary तत्व... 1 Electric Cars 2 SUV 3 AUV
मुख्य बिंदु:
StringDictionaryकुंजियों को लोअरकेस में बदल देता है (केस-इनसेंसिटिव)- पहले उदाहरण में कुंजियाँ "A"-"G" लोअरकेस "a"-"g" के रूप में प्रदर्शित होती हैं
- दूसरे उदाहरण में संख्यात्मक कुंजियाँ अपरिवर्तित रहती हैं
- कुंजी और मान दोनों
stringटाइप के होने चाहिए nullकुंजी की अनुमति नहीं है, लेकिनnullमान अनुमत है- आधुनिक C# में
Dictionary<string, string>को प्राथमिकता दी जाती है