C#程式:檢查雜湊表集合是否為空


C# 中的 Hashtable 集合是鍵值對的集合,這些鍵值對根據鍵的雜湊碼進行組織。雜湊碼是使用雜湊碼函式計算的。

雜湊表中的每個元素都是一個鍵值對,具有唯一的鍵。鍵也必須是非空的。值可以為空並重復。

在本文中,我們將討論如何檢查雜湊表集合是否為空。

如何檢查雜湊表集合是否為空?

C# 中實現雜湊表集合的類是 Hashtable 類。我們可以透過計算雜湊表中存在的元素數量來檢查雜湊表集合是否為空。

為此,我們可以使用 Hashtable 類的“Count”屬性,該屬性返回雜湊表中的元素數量。

因此,如果 Count 屬性返回 0,則表示雜湊表為空;如果它返回大於 0 的值,則表示雜湊表包含元素。

讓我們首先了解 Hashtable 類 Count 屬性的原型。

語法

public virtual int Count { get; }

返回值 − Int32 型別的屬性值

描述 − 獲取 Hashtable 中包含的鍵值對的數量。

名稱空間

System.Collections

從上面 Count 屬性的描述可以看出,我們可以使用此屬性獲取雜湊表集合中鍵值對的數量。

現在讓我們看幾個程式設計示例,這將幫助我們理解這個 Count 屬性。

示例

讓我們看看第一個關於如何檢查雜湊表是否為空的程式。程式如下所示。

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable myTable = new Hashtable();
      //get the count of items in hashtable
      int mySize = myTable.Count;
      if(mySize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("The hashtable is not empty. It has {0} item(s)", mySize);
   }   
}

在這個程式中,我們建立了一個 Hashtable 物件,並且沒有向其中新增任何元素。然後我們使用 Count 屬性檢索雜湊表中存在的元素數量。最後,評估 Count 屬性返回的值,並相應地顯示訊息,指示雜湊表是否為空。

輸出

程式生成以下輸出。

Hashtable is empty

由於雜湊表中沒有元素,因此顯示訊息表明雜湊表為空。

現在讓我們向上面的程式中的雜湊表新增一些元素。現在我們使用“Add()”方法向雜湊表新增兩個元素。

示例

程式如下所示。

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable myTable = new Hashtable();
      myTable.Add("1", "One");
      myTable.Add("2", "Two");
      //get the count of items in hashtable
      int mySize = myTable.Count;
      if(mySize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("The hashtable is not empty. It has {0} item(s).", mySize);
   }
}

輸出

在這裡,我們向雜湊表添加了兩個元素。現在輸出更改為如下所示。

The hashtable is not empty. It has 2 item(s)

正如我們所看到的,Count 屬性返回了雜湊表中元素的數量。

現在讓我們看另一個例子以便更好地理解。

示例

程式如下所示。

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable langCode = new Hashtable();
      langCode.Add("Perl", "");
      //get the count of items in hashtable
      int hashtabSize = langCode.Count;
      if(hashtabSize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("Hashtable is not empty. It has {0} item(s)", hashtabSize);
   }
}

輸出

這裡我們有一個包含一個元素的 langCode 雜湊表。我們再次使用 Count 屬性,它返回雜湊表中元素的數量。程式的輸出如下所示。

Hashtable is not empty. It has 1 item(s)

由於雜湊表中包含一個元素,因此相應地給出了訊息。現在讓我們刪除雜湊表中存在的元素。為此,我們只需註釋掉向雜湊表新增元素的行。

示例

程式將如下所示。

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable langCode = new Hashtable();
      //langCode.Add("Perl", "");
      //get the count of items in hashtable
      int hashtabSize = langCode.Count;
      if(hashtabSize == 0)
         Console.WriteLine("Hashtable is empty");
      else
         Console.WriteLine("Hashtable is not empty. It has {0} item(s)", hashtabSize);
   }
}

輸出

現在雜湊表中沒有任何元素。因此,當我們在這個雜湊表上使用 Count 屬性時,它返回零。因此,輸出顯示雜湊表為空。

Hashtable is empty

因此,由於 Hashtable 類中沒有直接的方法來檢查雜湊表是否為空,我們使用 Hashtable 類的 Count 屬性來獲取雜湊表中元素的數量。如果 Count 返回 0,則我們得出結論雜湊表為空。如果它返回非零值,則表示雜湊表中包含元素。

更新於:2022年12月21日

瀏覽量:1K+

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告