C# Hashtable 類的 Count 特性是什麼?


要查詢 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

更新於: 2020 年 6 月 20 日

91 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.