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

C# में OrderedDictionary कलेक्शन रीड-ओनली है या नहीं जांचना

OrderedDictionary.IsReadOnly प्रॉपर्टी यह दर्शाती है कि क्या संग्रह केवल पढ़ने योग्य है। डिफ़ॉल्ट रूप से OrderedDictionary रीड-ओनली नहीं होता (false लौटाता है)। AsReadOnly() मेथड से रीड-ओनली रैपर बनाया जा सकता है।

उदाहरण 1: IsReadOnly और ContainsKey जांच

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 values = dict.Values;
        string[] strValues = new string[dict.Count];
        values.CopyTo(strValues, 0);

        Console.WriteLine("वैल्यूज़ प्रदर्शित...");
        for (int i = 0; i < dict.Count; i++) {
            Console.WriteLine(strValues[i]);
        }

        // IsReadOnly जांचें
        Console.WriteLine("\nक्या OrderedDictionary रीड-ओनली है? = " + dict.IsReadOnly);
        
        // Contains - कुंजी मौजूदगी जांचें
        Console.WriteLine("क्या कुंजी '15' मौजूद है? = " + dict.Contains("15"));
        Console.WriteLine("क्या कुंजी '5' मौजूद है? = " + dict.Contains("5"));
    }
}

आउटपुट

वैल्यूज़ प्रदर्शित...
One
Two
Three
Four
Five
Six
Seven
Eight

क्या OrderedDictionary रीड-ओनली है? = False
क्या कुंजी '15' मौजूद है? = False
क्या कुंजी '5' मौजूद है? = True

उदाहरण 2: स्ट्रिंग कुंजियों के साथ

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

public class Demo {
    public static void Main() {
        OrderedDictionary dict = new OrderedDictionary();
        dict.Add("A", "Pen");
        dict.Add("B", "Pencil");
        dict.Add("C", "Notebook");
        dict.Add("D", "Table");
        dict.Add("E", "Chair");
        dict.Add("F", "Coworking");
        dict.Add("G", "Cubicle");
        dict.Add("H", "Sticky Notes");
        dict.Add("I", "WhiteBoard");
        dict.Add("J", "Marker");

        ICollection values = dict.Values;
        string[] strValues = new string[dict.Count];
        values.CopyTo(strValues, 0);

        Console.WriteLine("वैल्यूज़ प्रदर्शित...");
        for (int i = 0; i < dict.Count; i++) {
            Console.WriteLine(strValues[i]);
        }

        Console.WriteLine("\nक्या OrderedDictionary रीड-ओनली है? = " + dict.IsReadOnly);
        Console.WriteLine("क्या कुंजी 'B' मौजूद है? = " + dict.Contains("B"));
    }
}

आउटपुट

वैल्यूज़ प्रदर्शित...
Pen
Pencil
Notebook
Table
Chair
Coworking
Cubicle
Sticky Notes
WhiteBoard
Marker

क्या OrderedDictionary रीड-ओनली है? = False
क्या कुंजी 'B' मौजूद है? = True

रीड-ओनली रैपर बनाना

// रीड-ओनली रैपर प्राप्त करें
OrderedDictionary readOnlyDict = dict.AsReadOnly();

Console.WriteLine("रैपर रीड-ओनली है? = " + readOnlyDict.IsReadOnly); // True

// रैपर पर संशोधन का प्रयास - NotSupportedException फेंकेगा
// readOnlyDict.Add("K", "New"); // रनटाइम एरर

मुख्य बिंदु

  • IsReadOnly प्रॉपर्टी: डिफ़ॉल्ट false (संशोधन योग्य)।
  • AsReadOnly(): रीड-ओनली रैपर लौटाता है (IsReadOnly = true)।
  • Contains(key): कुंजी मौजूदगी जांचता है (केस-सेंसिटिव)।
  • Values और Keys प्रॉपर्टीज़ ICollection लौटाती हैं।
  • रीड-ओनली रैपर पर Add/Remove/Clear करने पर NotSupportedException फेंका जाता है।

नोट

OrderedDictionary लीगेसी नॉन-जेनेरिक क्लास है। नए कोड के लिए Dictionary<TKey, TValue> के साथ अलग ऑर्डर मेंटेनेंस (जैसे List<TKey>) या थर्ड-पार्टी OrderedDictionary<TKey, TValue> लाइब्रेरी उपयोग करें।

  1. - C# में Collection में किसी तत्व की जांच कैसे करें

    C# में Collection<T> किसी विशिष्ट तत्व को रखता है या नहीं, यह जांचने के लिए Contains() मेथड का उपयोग किया जाता है। यह मेथड एक बूलियन मान लौटाता है। उदाहरण 1 using System; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection col = new Col

  1. - C# में ऐरे केवल पढ़ने योग्य है या नहीं जांचें

    C# में एक ऐरे केवल पढ़ने योग्य है या नहीं, यह जांचने के लिए IsReadOnly प्रॉपर्टी का उपयोग किया जाता है। ऐरे के लिए यह प्रॉपर्टी हमेशा false लौटाती है क्योंकि ऐरे आकार में निश्चित होते हैं लेकिन संशोधन योग्य होते हैं। IsFixedSize प्रॉपर्टी हमेशा true लौटाती है। उदाहरण 1 ele.StartsWith(E)));

  1. - C# सूची संग्रह में कोई आइटम मौजूद है या नहीं कैसे जांचें

    एक सूची सेट करें: List list1 = new List() { Lawrence, Adams, Pitt, Tom }; अब Contains विधि का उपयोग करके जांचें कि कोई आइटम सूची में मौजूद है या नहीं। if (list1.Contains(Adams) == true) { Console.WriteLine(Item exists!); } C# सूची में कोई आइटम मौजूद है या नहीं जांचने का कोड निम्