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

C# में OrderedDictionary संग्रह में कुंजी और मान जोड़ें

C# में OrderedDictionary वर्ग की Add(key, value) विधि का उपयोग करके कुंजी-मान जोड़े जोड़े जा सकते हैं। यह संग्रह सम्मिलन क्रम को बनाए रखता है, जिससे तत्वों को उनके जोड़े जाने के क्रम में पुनरावृत्त किया जा सकता है।

उदाहरण 1: दो OrderedDictionary बनाना और तुलना

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict1 = new OrderedDictionary();
        dict1.Add("A", "Books");
        dict1.Add("B", "Electronics");
        dict1.Add("C", "Smart Wearables");
        dict1.Add("D", "Pet Supplies");
        dict1.Add("E", "Clothing");
        dict1.Add("F", "Footwear");
        
        Console.WriteLine("OrderedDictionary1 तत्व...");
        foreach(DictionaryEntry d in dict1) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        OrderedDictionary dict2 = new OrderedDictionary();
        dict2.Add("1", "One");
        dict2.Add("2", "Two");
        dict2.Add("3", "Three");
        dict2.Add("4", "Four");
        dict2.Add("5", "Five");
        dict2.Add("6", "Six");
        
        Console.WriteLine("\nOrderedDictionary2 तत्व...");
        foreach(DictionaryEntry d in dict2) {
            Console.WriteLine(d.Key + " " + d.Value);
        }
        
        Console.WriteLine("\nक्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = " + (dict1.Equals(dict2)));
    }
}

आउटपुट

OrderedDictionary1 तत्व...
A Books
B Electronics
C Smart Wearables
D Pet Supplies
E Clothing
F Footwear

OrderedDictionary2 तत्व...
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
क्या OrderedDictionary1, OrderedDictionary2 के बराबर है? = False

उदाहरण 2: मानों का संग्रह प्राप्त करना और अन्य प्रॉपर्टी जांचना

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("1", "One");
        dict.Add("2", "Two");
        dict.Add("3", "Three");
        dict.Add("4", "Four");
        dict.Add("5", "Five");
        dict.Add("6", "Six");
        dict.Add("7", "Seven");
        dict.Add("8", "Eight");
        
        ICollection col = dict.Values;
        String[] strVal = new String[dict.Count];
        col.CopyTo(strVal, 0);
        
        Console.WriteLine("मान प्रदर्शित हो रहे हैं...");
        for (int i = 0; i < dict.Count; i++) {
            Console.WriteLine(strVal[i]);
        }
        
        Console.WriteLine("क्या OrderedDictionary केवल-पढ़ने योग्य है? = " + dict.IsReadOnly);
        Console.WriteLine("क्या OrderedDictionary में कुंजी 15 है? = " + dict.Contains("15"));
        Console.WriteLine("क्या OrderedDictionary में कुंजी 5 है? = " + dict.Contains("5"));
    }
}

आउटपुट

मान प्रदर्शित हो रहे हैं...
One
Two
Three
Four
Five
Six
Seven
Eight
क्या OrderedDictionary केवल-पढ़ने योग्य है? = False
क्या OrderedDictionary में कुंजी 15 है? = False
क्या OrderedDictionary में कुंजी 5 है? = True

मुख्य बिंदु

  • OrderedDictionary सम्मिलन क्रम बनाए रखता है
  • Add(key, value) से तत्व जोड़े जाते हैं
  • Values प्रॉपर्टी से सभी मानों का ICollection प्राप्त होता है
  • Contains(key) से कुंजी की उपस्थिति जांच सकते हैं
  • IsReadOnly प्रॉपर्टी संग्रह के केवल-पढ़ने योग्य होने को दर्शाती है
  1. - C# वैल्यू टपल

    C# टपल का एक वैल्यू टाइप प्रतिनिधित्व वैल्यू टाइप टपल है। इसे C# 7.0 में पेश किया गया था।नोट: ValueTuple प्रोग्राम चलाने के लिए System.ValueTuple पैकेज जोड़ें।आइए देखें कि इसे कैसे जोड़ा जाए:अपने प्रोजेक्ट पर जाएंसॉल्यूशन एक्सप्लोरर में प्रोजेक्ट पर राइट-क्लिक करेंManage NuGet Packages चुनेंआप NuGet

  1. - C# में Hashtable और Dictionary के साथ कार्य करना

    HashtableHashtable क्लास की-वैल्यू पेयर्स के संग्रह को दर्शाता है जो की के हैश कोड के आधार पर व्यवस्थित होते हैं। यह संग्रह में एलिमेंट्स को एक्सेस करने के लिए की का उपयोग करता है।Hashtable क्लास में आमतौर पर उपयोग होने वाले कुछ मेथड्स:क्रमांकमेथड और विवरण1public virtual void Add(object key, object

  1. - पायथन में डिक्शनरी में कुंजी-मान जोड़ी जोड़ना

    पायथन डिक्शनरी कुंजी-मान जोड़े का एक अनऑर्डर्ड संग्रह है। इस ट्यूटोरियल में हम देखेंगे कि पहले से परिभाषित डिक्शनरी में नई कुंजी-मान जोड़े कैसे जोड़ सकते हैं। नीचे दो दृष्टिकोण हैं जिनका हम उपयोग कर सकते हैं। सबस्क्रिप्ट के रूप में नई कुंजी असाइन करना हम एक नई कुंजी को सबस्क्रिप्ट के रूप में उपयोग