ElementAt(int index) अनुक्रम में निर्दिष्ट **शून्य-आधारित इंडेक्स** पर तत्व लौटाता है।
उदाहरण: रैंडम नाम चुनना
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
string[] str = { "Jack", "Pat", "David" };
Random r = new Random(DateTime.Now.Second);
// रैंडम इंडेक्स पर तत्व
string res = str.AsQueryable().ElementAt(r.Next(0, str.Length));
Console.WriteLine("रैंडम नाम = '{0}'", res);
}
}
आउटपुट
रैंडम नाम = 'Jack'
संस्करण
ElementAt(index): इंडेक्स पर तत्व, सीमा बाहर परArgumentOutOfRangeException।ElementAtOrDefault(index): सीमा बाहर पर डिफ़ॉल्ट मान (null/0)।
प्रदर्शन
IList<T>परO(1)(सीधा इंडेक्स एक्सेस)।- अन्य
IEnumerable<T>परO(index)(iteration)।
नोट
- ऐरे/लिस्ट के लिए सीधे
arr[index]तेज़ और सरल है। ElementAtLINQ क्वेरी चेन में उपयोगी जब स्रोतIEnumerableहो।