परिचय
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का उपयोग करें बजाय एन्यूमरेशन के।