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

C# में SortedDictionary.Count प्रॉपर्टी - कुंजी-मान जोड़ों की संख्या प्राप्त करें

परिचय

C# में SortedDictionary.Count प्रॉपर्टी SortedDictionary<TKey, TValue> में मौजूद कुंजी-मान जोड़ों की संख्या प्राप्त करती है। यह एक केवल-पढ़ने योग्य (read-only) प्रॉपर्टी है।

सिंटैक्स

public int Count { get; }

उदाहरण 1: जोड़ना, जांचना, क्लियर करना

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("SortedDictionary कुंजी-मान जोड़ों की संख्या = " + sortedDict.Count);

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

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

आउटपुट

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 कुंजी-मान जोड़ों की संख्या = 6
क्या SortedDictionary में कुंजी 200 है? = True
SortedDictionary कुंजी-मान जोड़ों की संख्या = 0

उदाहरण 2: डायनामिक जोड़ और क्लियर

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

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

        sortedDict.Add(100, "Inspiron");
        sortedDict.Add(200, "Alienware");
        sortedDict.Add(300, "Projectors");
        sortedDict.Add(400, "XPS");

        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(800, "Notebook");
        sortedDict.Add(10000, "Bluetooth Speaker");

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

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

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

आउटपुट

SortedDictionary कुंजी-मान जोड़े...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
SortedDictionary कुंजी-मान जोड़ों की संख्या = 4

SortedDictionary कुंजी-मान जोड़े...अपडेटेड
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 6
SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0

मुख्य बिंदु

  • O(1) ऑपरेशन - आंतरिक काउंटर बनाए रखता है।
  • Add() बढ़ाता है, Remove() घटाता है, Clear() शून्य पर सेट करता है।
  • डुप्लीकेट कुंजियों की अनुमति नहीं - Add() डुप्लीकेट पर ArgumentException फेंकता है।
  • Count == 0 जांचने के लिए Count का उपयोग करें बजाय एन्यूमरेशन के।
  1. HTML DOM KeyboardEvent location गुण HTML DOM KeyboardEvent location गुण

    HTML DOM KeyboardEvent location गुण कीबोर्ड पर दबाई गई कुंजी के स्थान से संबंधित संख्या लौटाता है। सिंटैक्स निम्नलिखित सिंटैक्स है − दबाई गई कुंजी का स्थान लौटाना − event.location संख्याएं यहां, लौटाई गई संख्या निम्नलिखित हो सकती है − संख्या विवरण 0 यह कीबोर्ड पर लगभग सभी मानों को दर्शाता है

  1. HTML DOM KeyboardEvent key गुण HTML DOM KeyboardEvent key गुण

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

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

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