परिचय
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))।