Để tìm số phần tử của lớp Hashtable, hãy sử dụng thuộc tính Count. Đầu tiên, đặt lớp Hashtable với các phần tử -
Hashtable ht = new Hashtable();
ht.Add("One", "Tom");
ht.Add("Two", "Jack");
ht.Add("Three", "Peter");
ht.Add("Four", "Russel");
ht.Add("Five", "Brad");
ht.Add("Six", "Bradley");
ht.Add("Seven", "Steve");
ht.Add("Eight", "David"); Bây giờ, hãy đếm số phần tử bằng thuộc tính Count -
ht.Count
Sau đây là ví dụ đầy đủ để tìm hiểu về cách sử dụng thuộc tính Hashtable Count trong C #.
Ví dụ
using System;
using System.Collections;
namespace Demo {
class Program {
static void Main(string[] args) {
Hashtable ht = new Hashtable();
ht.Add("One", "Tom");
ht.Add("Two", "Jack");
ht.Add("Three", "Peter");
ht.Add("Four", "Russel");
ht.Add("Five", "Brad");
ht.Add("Six", "Bradley");
ht.Add("Seven", "Steve");
ht.Add("Eight", "David");
Console.WriteLine("Count = " + ht.Count);
Console.ReadKey();
}
}
} Đầu ra
Count = 8