若要查找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
以下是学习C#中Hashtable Count属性用法的完整示例。
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