Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में StringDictionary को ऐरे में निर्दिष्ट इंडेक्स पर कॉपी करना

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[] आवश्यक
  1. - C# में SortedList से निर्दिष्ट सूचकांक पर तत्व हटाएं

    C# में SortedList वर्ग की RemoveAt(int index) विधि का उपयोग करके निर्दिष्ट सूचकांक पर स्थित कुंजी-मान जोड़े को हटाया जा सकता है। चूंकि SortedList कुंजियों के आधार पर क्रमबद्ध होता है, सूचकांक क्रमबद्ध क्रम को दर्शाता है, सम्मिलन क्रम नहीं। उदाहरण 1: सूचकांक 3 पर तत्व हटाना using System; using Syste

  1. - C# में Array Class के Copy(,,) Method का उपयोग कैसे करें

    जैसा कि नाम से पता चलता है, C# में Array.Copy() Method का उपयोग एक Array के Elements को दूसरे Array में Copy करने के लिए किया जाता है।निम्न Syntax है:Array.Copy(src, dest, length);यहांsrc = Copy किया जाने वाला Arraydest = Destination Arraylength = Copy किए जाने वाले Elements की संख्यानिम्न Example,

  1. - C# में ऐरे कॉपी (Array Copy)

    C# में एक ऐरे के सेक्शन को दूसरे ऐरे में कॉपी करने के लिए Array.Copy मेथड का उपयोग करें।हमारे मूल ऐरे में 10 एलिमेंट्स हैं:int[] n = new int[10]; /* n 10 इंटीजर्स का ऐरे है */हमारा नया ऐरे जो पहले ऐरे के सेक्शन को कॉपी करेगा, उसमें 5 एलिमेंट्स हैं:int[] m = new int[5]; /* m 5 इंटीजर्स का ऐरे है */Ar