StringDictionary.CopyTo() मेथड स्ट्रिंग डिक्शनरी के की-वैल्यू पेयर्स को DictionaryEntry ऐरे में कॉपी करता है। StringDictionary कुंजियों को लोअरकेस में स्टोर करता है।
उदाहरण 1: इंडेक्स 0 से कॉपी करना (सटीक आकार)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "SUV");
strDict.Add("2", "AUV");
strDict.Add("3", "Electric Car");
strDict.Add("4", "Utility Vehicle");
strDict.Add("5", "Hatchback");
strDict.Add("6", "Compact car");
strDict.Add("7", "MUV");
strDict.Add("8", "Crossover");
strDict.Add("9", "Convertible");
strDict.Add("10", "Quadricycle");
// 10 आकार का DictionaryEntry ऐरे
DictionaryEntry[] arr = new DictionaryEntry[10];
// इंडेक्स 0 से कॉपी करें
strDict.CopyTo(arr, 0);
Console.WriteLine("ऐरे में कॉपी किए गए तत्व...");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i].Key + " " + arr[i].Value);
}
}
}
आउटपुट
ऐरे में कॉपी किए गए तत्व...
10 Quadricycle
1 SUV
2 AUV
3 Electric Car
4 Utility Vehicle
5 Hatchback
6 Compact car
7 MUV
8 Crossover
9 Convertible
उदाहरण 2: इंडेक्स 2 से कॉपी करना (बड़े ऐरे में)
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "SUV");
strDict.Add("2", "AUV");
strDict.Add("3", "Electric Car");
strDict.Add("4", "Utility Vehicle");
strDict.Add("5", "Hatchback");
strDict.Add("6", "Compact car");
// 8 आकार का ऐरे, इंडेक्स 2 से कॉपी करें
DictionaryEntry[] arr = new DictionaryEntry[8];
strDict.CopyTo(arr, 2);
Console.WriteLine("ऐरे में कॉपी किए गए तत्व (इंडेक्स 2 से)...");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i].Key + " " + arr[i].Value);
}
}
}
आउटपुट
ऐरे में कॉपी किए गए तत्व (इंडेक्स 2 से)...
1 SUV
2 AUV
3 Electric Car
4 Utility Vehicle
5 Hatchback
6 Compact car
महत्वपूर्ण विशेषताएं
- केस-इनसेंसिटिव कुंजियाँ: StringDictionary कुंजियों को लोअरकेस में बदल देता है।
- कॉपी क्रम: अनिर्धारित (हैशटेबल-आधारित आंतरिक कार्यान्वयन)।
- लक्ष्य ऐरे:
DictionaryEntry[]होना चाहिए। - इंडेक्स पैरामीटर: लक्ष्य ऐरे में प्रारंभिक स्थिति।
- लीगेसी क्लास:
StringDictionaryपुराना है; नए कोड के लिएDictionary<string, string>उपयोग करें।
DictionaryEntry संरचना
public struct DictionaryEntry {
public object Key { get; set; }
public object Value { get; set; }
}
आधुनिक विकल्प
// Dictionary<string, string> के साथ (अनुशंसित)
var dict = new Dictionary<string, string>();
dict["1"] = "SUV";
dict["2"] = "AUV";
// KeyValuePair<string, string>[] ऐरे में कॉपी
var arr = new KeyValuePair<string, string>[dict.Count];
dict.CopyTo(arr, 0); // Dictionary.CopyTo के लिए KeyValuePair[] आवश्यक