SortedList.GetByIndex(int index) मेथड निर्दिष्ट शून्य-आधारित इंडेक्स पर मान लौटाता है, जहां इंडेक्स क्रमबद्ध कुंजी क्रम से मेल खाता है। यह कुंजी नहीं बल्कि मान लौटाता है।
उदाहरण 1: स्ट्रिंग कुंजियाँ और मान
using System;
using System.Collections;
public class Demo
{
public static void Main(String[] args)
{
SortedList list = new SortedList();
list.Add("A", "Jacob");
list.Add("B", "Sam");
list.Add("C", "Tom");
list.Add("D", "John");
list.Add("E", "Tim");
list.Add("F", "Mark");
list.Add("G", "Gary");
Console.WriteLine("Value at index 2 = " + list.GetByIndex(2));
Console.WriteLine("Value at index 5 = " + list.GetByIndex(5));
Console.WriteLine("Value at index 6 = " + list.GetByIndex(6));
}
}
आउटपुट
Value at index 2 = Tom Value at index 5 = Mark Value at index 6 = Gary
उदाहरण 2: पूर्णांक कुंजियाँ
using System;
using System.Collections;
public class Demo
{
public static void Main(String[] args)
{
SortedList list = new SortedList();
list.Add(1, "One");
list.Add(2, "Two");
list.Add(3, "Three");
list.Add(4, "Four");
list.Add(5, "Five");
Console.WriteLine("Value at index 0 = " + list.GetByIndex(0));
}
}
आउटपुट
Value at index 0 = One
GetByIndex O(1) है। अमान्य इंडेक्स (नकारात्मक या >= Count) ArgumentOutOfRangeException फेंकता है। संबंधित मेथड: GetKey(int index) कुंजी लौटाता है, IndexOfKey/IndexOfValue कुंजी/मान का इंडेक्स ढूंढते हैं।