หากต้องการค้นหาจำนวนองค์ประกอบของคลาส Hashtable ให้ใช้คุณสมบัติ Count ขั้นแรก ตั้งค่าคลาส Hashtable ด้วยองค์ประกอบ -
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"); ตอนนี้ นับจำนวนองค์ประกอบโดยใช้คุณสมบัติ Count -
ht.Count
ต่อไปนี้คือตัวอย่างที่สมบูรณ์เพื่อเรียนรู้เกี่ยวกับการใช้คุณสมบัติ Hashtable Count ใน C#
ตัวอย่าง
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();
}
}
} ผลลัพธ์
Count = 8