C# में SortedList वर्ग की IndexOfValue(object value) विधि का उपयोग करके निर्दिष्ट मान का शून्य-आधारित सूचकांक प्राप्त किया जा सकता है। यह विधि मान मिलने पर उसकी पहली घटना का सूचकांक लौटाती है, अन्यथा -1 लौटाती है। सूचकांक क्रमबद्ध कुंजी क्रम पर आधारित होता है।
उदाहरण 1: संख्यात्मक मानों और श्रेणियों के साथ दो SortedList
using System;
using System.Collections;
public class Demo {
public static void Main(String[] args) {
SortedList list1 = new SortedList();
list1.Add("One", 1);
list1.Add("Two ", 2);
list1.Add("Three ", 3);
list1.Add("Four", 4);
list1.Add("Five", 5);
list1.Add("Six", 6);
list1.Add("Seven ", 7);
list1.Add("Eight ", 8);
list1.Add("Nine", 9);
list1.Add("Ten", 10);
Console.WriteLine("SortedList1 तत्व...");
foreach(DictionaryEntry d in list1) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.Write("मान 7 पर सूचकांक = " + list1.IndexOfValue(7));
SortedList list2 = new SortedList();
list2.Add("A", "Accessories");
list2.Add("B", "Books");
list2.Add("C", "Smart Wearable Tech");
list2.Add("D", "Home Appliances");
Console.WriteLine("\n\nSortedList2 तत्व...");
foreach(DictionaryEntry d in list2) {
Console.WriteLine(d.Key + " " + d.Value);
}
Console.Write("मान Books पर सूचकांक = " + list2.IndexOfValue("Books"));
}
}
आउटपुट
SortedList1 तत्व... Eight 8 Five 5 Four 4 Nine 9 One 1 Seven 7 Six 6 Ten 10 Three 3 Two 2 मान 7 पर सूचकांक = 5 SortedList2 तत्व... A Accessories B Books C Smart Wearable Tech D Home Appliances मान Books पर सूचकांक = 1
उदाहरण 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मान Marketing पर सूचकांक = " + list.IndexOfValue("Marketing"));
Console.WriteLine("मान Purchase पर सूचकांक = " + list.IndexOfValue("Purchase"));
Console.WriteLine("मान Sales पर सूचकांक = " + list.IndexOfValue("Sales"));
Console.WriteLine("मान IT पर सूचकांक = " + list.IndexOfValue("IT"));
Console.WriteLine("मान Operations पर सूचकांक = " + list.IndexOfValue("Operations"));
}
}
आउटपुट
SortedList तत्व... Five Operations Four Purchase One Finance Six IT Three Sales Two Marketing मान Marketing पर सूचकांक = 5 मान Purchase पर सूचकांक = 1 मान Sales पर सूचकांक = 4 मान IT पर सूचकांक = 3 मान Operations पर सूचकांक = 0
मुख्य बिंदु
IndexOfValue(value)मान की पहली घटना का शून्य-आधारित सूचकांक लौटाता है- मान नहीं मिलने पर -1 लौटाता है
- यदि एक ही मान एकाधिक कुंजियों से जुड़ा है, तो पहली घटना (क्रमबद्ध कुंजी क्रम में) का सूचकांक लौटाया जाता है
- सूचकांक क्रमबद्ध कुंजी क्रम पर आधारित होता है
GetByIndex(index)से सूचकांक पर मान प्राप्त कर सकते हैं