परिचय
C# में SortedDictionary.Item[] प्रॉपर्टी (इंडेक्सर) निर्दिष्ट कुंजी से जुड़े मान को प्राप्त या सेट करने के लिए उपयोग की जाती है। यह dict[key] सिंटैक्स को सक्षम करती है।
सिंटैक्स
public TValue this[TKey key] { get; set; }
key प्राप्त या सेट किए जाने वाले मान की कुंजी है।
उदाहरण 1: बुनियादी उपयोग और Clear
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main() {
SortedDictionary sortedDict = new SortedDictionary();
sortedDict.Add(1, "One");
sortedDict.Add(2, "Two");
sortedDict.Add(3, "Three");
sortedDict.Add(4, "Four");
sortedDict.Add(5, "Five");
sortedDict.Add(6, "Six");
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.Clear();
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
}
}
आउटपुट
Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six SortedDictionary कुंजी-मान जोड़ों की संख्या = 6 SortedDictionary कुंजी-मान जोड़े...अपडेटेड Key = 1, Value = One Key = 2, Value = Two Key = 3, Value = Three Key = 4, Value = Four Key = 5, Value = Five Key = 6, Value = Six Key = 7, Value = Seven Key = 8, Value = Eight SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 8 कुंजी पांच का मान = Five 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(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]);
sortedDict.Clear();
Console.WriteLine("\nSortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = " + sortedDict.Count);
}
}
आउटपुट
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 SortedDictionary कुंजी-मान जोड़ों की संख्या (अपडेटेड) = 0
महत्वपूर्ण व्यवहार
- Get: यदि कुंजी मौजूद नहीं है तो
KeyNotFoundExceptionफेंकता है। सुरक्षित पहुंच के लिएTryGetValue()का उपयोग करें। - Set: यदि कुंजी मौजूद है तो मान अपडेट करता है, अन्यथा नया जोड़ता है (
Add()के समान लेकिन अपवाद नहीं फेंकता यदि कुंजी मौजूद है)। - यही कारण है कि इसे "इंडेक्सर" कहा जाता है - यह ऐरे-जैसी पहुंच सक्षम करता है।