ElementAt() เป็นเมธอด System.Linq ใน C# ที่ใช้เพื่อรับและแสดงองค์ประกอบที่ดัชนีเฉพาะ
ต่อไปนี้เป็นสตริงอาร์เรย์ของเรา -
string[] arr = { "One", "Two", "Three", "Four", "Five" }; ตอนนี้เพื่อรับองค์ประกอบที่ดัชนี 0 ให้ใช้วิธี ElementAt() -
arr.ElementAt(0);
ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System.IO;
using System;
using System.Linq;
public class Demo {
public static void Main() {
string[] arr = { "One", "Two", "Three", "Four", "Five" };
// displaying element at index 0
string res = arr.ElementAt(0);
Console.WriteLine(res);
}
} ผลลัพธ์
One