ขั้นแรก ตั้งค่าอาร์เรย์สตริง
string[] num = { "One", "Two", "Three", "Four", "Five"}; ใช้วิธี Linq LongCount เพื่อรับการนับองค์ประกอบ
num.AsQueryable().LongCount();
นี่คือรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
string[] num = { "One", "Two", "Three", "Four", "Five"};
long res = num.AsQueryable().LongCount();
Console.WriteLine("{0} elements", res);
}
} ผลลัพธ์
5 elements