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

C# में SortedDictionary.Keys प्रॉपर्टी - कुंजियों का संग्रह प्राप्त करें

परिचय

C# में SortedDictionary.Keys प्रॉपर्टी SortedDictionary<TKey, TValue> में मौजूद सभी कुंजियों (keys) का एक संग्रह वापस करती है। कुंजियां सॉर्टेड क्रम में वापस की जाती हैं।

सिंटैक्स

public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; }

उदाहरण 1: वाहन प्रकारों के साथ

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "SUV");
        sortedDict.Add(2, "MUV");
        sortedDict.Add(3, "Utility Vehicle");
        sortedDict.Add(4, "AUV");
        sortedDict.Add(5, "Hatchback");
        sortedDict.Add(6, "Convertible");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

        sortedDict.Add(7, "Seven");
        sortedDict.Add(8, "Eight");

        Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़े...अपडेटेड");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);

        Console.WriteLine("कुंजी पांच का मान = " + sortedDict[5]);
        sortedDict[5] = "Crossover";
        Console.WriteLine("कुंजी पांच का मान (अपडेटेड) = " + sortedDict[5]);

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nकुंजियां...");
        foreach (int i in keyColl)
            Console.WriteLine(i);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
SortedDictionary कुंजी-मान जोड़ों की संख्या = 6

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 1, Value = SUV
Key = 2, Value = MUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = AUV
Key = 5, Value = Hatchback
Key = 6, Value = Convertible
Key = 7, Value = Seven
Key = 8, Value = Eight
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8
कुंजी पांच का मान = Hatchback
कुंजी पांच का मान (अपडेटेड) = Crossover

कुंजियां...
1
2
3
4
5
6
7
8

उदाहरण 2: ContainsKey के साथ

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo {
    public static void Main() {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(100, "Mobile");
        sortedDict.Add(200, "Laptop");
        sortedDict.Add(300, "Desktop");
        sortedDict.Add(400, "Speakers");
        sortedDict.Add(500, "Headphone");
        sortedDict.Add(600, "Earphone");

        Console.WriteLine("SortedDictionary कुंजी-मान जोड़े...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);

        Console.WriteLine("\nक्या SortedDictionary में कुंजी 200 है? = " + sortedDict.ContainsKey(200));

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nकुंजियां...");
        foreach (int i in keyColl)
            Console.WriteLine(i);
    }
}

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

क्या SortedDictionary में कुंजी 200 है? = True

कुंजियां...
100
200
300
400
500
600

मुख्य बिंदु

  • Keys प्रॉपर्टी KeyCollection वापस करती है जो IEnumerable<TKey> इम्प्लीमेंट करता है।
  • कुंजियां हमेशा सॉर्टेड क्रम में होती हैं (TKey के तुलनाकर्ता के अनुसार)।
  • संग्रह डायनामिक है - डिक्शनरी में परिवर्तन प्रतिबिंबित होते हैं।
  • केवल पढ़ने योग्य - आप इस संग्रह के माध्यम से कुंजियों को संशोधित नहीं कर सकते।
  • ContainsKey() का उपयोग कुंजी मौजूदगी जांचने के लिए करें (O(log n))।
  1. HTML DOM KeyboardEvent code गुण HTML DOM KeyboardEvent code गुण

    KeyboardEvent code गुण घटना का उपयोग करके दबाई गई कुंजी से संबंधित कुंजी पहचानकर्ता लौटाता है। नोट − सटीक परिणामों के लिए key गुण का उपयोग करें सिंटैक्स निम्नलिखित सिंटैक्स है − नवीनतम टाइप किए गए वर्ण का कोड लौटाना − event.code उदाहरण आइए KeyboardEvent code गुण का एक उदाहरण देखें − <!DOCTYPE ht

  1. C# में Console.KeyAvailable() प्रॉपर्टी C# में Console.KeyAvailable() प्रॉपर्टी

    C# में Console.KeyAvailable() प्रॉपर्टी का उपयोग यह दर्शाने वाला मान प्राप्त करने के लिए किया जाता है कि क्या इनपुट स्ट्रीम में कोई कुंजी दबाने की घटना उपलब्ध है। सिंटैक्स सिंटैक्स निम्नलिखित है: public static bool KeyAvailable { get; } उदाहरण आइए अब C# में Console.KeyAvailable() प्रॉपर्टी को लागू

  1. Chromebook फ़ंक्शन कुंजियों का उपयोग कैसे करें Chromebook फ़ंक्शन कुंजियों का उपयोग कैसे करें

    Chromebook मालिक अच्छी तरह से जानते हैं कि उनके कीबोर्ड सामान्य से बहुत दूर हैं। सर्च बार के समावेश और ढेर सारी कुंजियों (पूरी फ़ंक्शन कुंजी पंक्ति और कैप्स लॉक कुंजी सहित) के बहिष्करण से, Chromebook पर कीबोर्ड को अपनाने में कुछ समय लगता है। जबकि Google ने क्लासिक फ़ंक्शन कुंजियों को साहसपूर्वक छोड़