C# में SortedList केवल-पढ़ने योग्य है या नहीं यह जांचने के लिए, हम IsReadOnly प्रॉपर्टी का उपयोग करते हैं। SortedList कुंजी-मान जोड़े का एक संग्रह है जो कुंजियों के आधार पर स्वचालित रूप से क्रमबद्ध होता है। यह System.Collections नेमस्पेस में स्थित एक गैर-जेनेरिक संग्रह है।
उदाहरण 1: SortedList के गुणों की जांच
using System;
using System.Collections;
public class Demo
{
public static void Main(String[] args)
{
SortedList list = new SortedList();
list.Add("One", "IT");
list.Add("Two", "Operations");
list.Add("Three", "Marketing");
list.Add("Four", "Purchase");
list.Add("Five", "Sales");
list.Add("Six", "Finance");
Console.WriteLine("SortedList तत्व...");
foreach (DictionaryEntry d in list)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nमानों की सूची...SortedList");
IList col = list.GetValueList();
foreach (string res in col)
{
Console.WriteLine(res);
}
Console.WriteLine("\nSortedList केवल-पढ़ने योग्य है? = " + list.IsReadOnly);
}
}
आउटपुट
SortedList तत्व... Five Sales Four Purchase One IT Six Finance Three Marketing Two Operations मानों की सूची...SortedList Sales Purchase IT Finance Marketing Operations SortedList केवल-पढ़ने योग्य है? = False
उदाहरण 2: कुंजियों की सूची और केवल-पढ़ने योग्य जांच
using System;
using System.Collections;
public class Demo
{
public static void Main(String[] args)
{
SortedList list = new SortedList();
list.Add("One", "Finance");
list.Add("Two", "Marketing");
list.Add("Three", "Sales");
list.Add("Four", "Purchase");
list.Add("Five", "Operations");
list.Add("Six", "IT");
Console.WriteLine("SortedList तत्व...");
foreach (DictionaryEntry d in list)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("\nकुंजी One पर इंडेक्स = " + list.IndexOfKey("One"));
ICollection col = list.Keys;
Console.WriteLine("\nकुंजियों का संग्रह...");
foreach (string res in col)
Console.WriteLine(res);
Console.WriteLine("\nSortedList केवल-पढ़ने योग्य है? = " + list.IsReadOnly);
}
}
आउटपुट
SortedList तत्व... Five Operations Four Purchase One Finance Six IT Three Sales Two Marketing कुंजी One पर इंडेक्स = 2 कुंजियों का संग्रह... Five Four One Six Three Two SortedList केवल-पढ़ने योग्य है? = False
मुख्य बिंदु:
IsReadOnlyहमेशाFalseलौटाता है मानकSortedListके लिए - यह संशोधन योग्य हैGetValueList()मानों की एक सूची लौटाता है (क्रमबद्ध कुंजियों के अनुसार)Keysप्रॉपर्टी कुंजियों का संग्रह लौटाती हैIndexOfKey()निर्दिष्ट कुंजी का शून्य-आधारित इंडेक्स लौटाता है- केवल-पढ़ने योग्य रैपर के लिए
SortedList.Synchronized()या कस्टम रैपर का उपयोग करें