C# 中 Hashtable 类的 Count 属性是什么?

csharpprogrammingserver side programming更新于 2025/10/15 15:07:17

要获取 Hashtable 类元素的数量,请使用 Count 属性。首先,将元素数量设置为 −

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

相关文章