Đầu tiên, đặt một mảng chuỗi.
string[] num = { "One", "Two", "Three", "Four", "Five"}; Sử dụng phương thức Linq LongCount để tính số phần tử.
num.AsQueryable().LongCount();
Đây là mã hoàn chỉnh -
Ví dụ
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);
}
} Đầu ra
5 elements