如何在 C# 中从 Hashtable 集合中删除项目?
C# 中的 Hashtable 是基于键的哈希码组织的键值对的集合。使用键访问哈希表中的项目。C# 的 Hashtable 类是实现哈希表的类。
使用此类,我们可以借助提供的构造函数创建一个新的哈希表对象。Hashtable 类还提供了各种方法,我们可以使用这些方法对哈希表执行各种操作。这些操作包括添加项目、检查指定键是否存在、计算项目数量、从哈希表中删除项目等等。
在本文中,我们将讨论根据指定键从哈希表集合中删除项目。
如何从哈希表集合中删除项目?
Hashtable类提供了一种方法"Remove"来从哈希表集合中删除项目。给定一个键,Remove 方法将从哈希表中删除具有指定键的项目。
Remove 方法的原型如下。
语法
public virtual void Remove (object key);
参数
Key − 要从哈希表集合中删除的元素的键。这是 System.Object 类型。
实现
IDictionary 接口的 Remove(Object) 方法。
异常
ArgumentNullException − 如果指定的键为空,则会引发此异常。
NotSupportedException − 如果哈希表具有固定大小或为只读,则会引发此异常。
如果哈希表中不存在指定的键,"Remove()"方法不会引发任何异常。如果密钥不存在,哈希表将保持不变,程序将成功执行。
Remove() 方法的编程示例
示例 1
以下程序演示了如何使用 Remove() 方法从哈希表集合中删除项目。
using System; using System.Collections; class MyHashTable { public static void Main(){ // Creating a Hashtable Hashtable numberNames = new Hashtable(); // Adding elements in Hashtable numberNames.Add("2", "Two"); numberNames.Add("13", "Thirteen"); numberNames.Add("24", "Twenty Four"); numberNames.Add("59", "Fifty Nine"); // Print the contents of Hashtable Console.WriteLine("**********Contents of Hashtable**********"); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } //read the key for which element is to be deleted Console.WriteLine("
Enter the key for which the element is to be removed from Hashtable "); string key = (string)Console.ReadLine(); //remove the element numberNames.Remove(key); //display the hashtable after deletion Console.WriteLine("
******Contents of Hashtable(after deletion)******"); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } } }
在此程序中,首先,我们创建了一个哈希表,以数字为键,以相应的数字名称为值。然后,哈希表显示在屏幕上。接下来,提示用户输入要从哈希表中删除其元素的键。输入键后,将使用此键作为参数调用 Remove() 方法。接下来,再次显示哈希表内容。如果哈希表中存在该键,则 Remove() 方法将删除该元素,否则,哈希表将保持不变。
输出
该程序生成以下输出。
**********Contents of Hashtable********** key =59, Value = Fifty Nine key =24, Value = Twenty Four key =13, Value = Thirteen key =2, Value = Two Enter the key for which the element is to be removed from Hashtable 13 ******Contents of Hashtable(after deletion)****** key =59, Value = Fifty Nine key =24, Value = Twenty Four key =2, Value = Two
上面的输出显示了删除前后哈希表内容之间的差异。
现在让我们看看当用户输入的键在哈希表中不存在时输出如何变化。在这种情况下,如前所述,哈希表保持不变,不会引发任何异常。以下是上述程序生成的输出。
输出
**********Contents of Hashtable********** key =59, Value = Fifty Nine key =24, Value = Twenty Four key =13, Value = Thirteen key =2, Value = Two Enter the key for which the element is to be removed from Hashtable 3 ******Contents of Hashtable(after deletion)****** key =59, Value = Fifty Nine key =24, Value = Twenty Four key =13, Value = Thirteen key =2, Value = Two
此处,用户输入了 key = 3,但哈希表中不存在该 key。在这种情况下,哈希表保持不变,因为 Remove() 方法不会删除任何元素。
示例 2
现在让我们考虑另一个哈希表删除示例。下面给出了相同的程序。
using System; using System.Collections; public class myHashtable{ public static void Main(){ // Create a new Hashtable. var tongueTwister = new Hashtable(); tongueTwister.Add("1a", "She"); tongueTwister.Add("1b", "sells"); tongueTwister.Add("1c", "sea"); tongueTwister.Add("2a", "shells"); tongueTwister.Add("2b", "on"); tongueTwister.Add("2c", "the"); tongueTwister.Add("3a", "sea"); tongueTwister.Add("3b", "shore"); // Displays the Hashtable. Console.WriteLine("The Hashtable initially contains the following:
"); foreach (DictionaryEntry elem in tongueTwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); // Removes the element with the specified key. string key = "3b"; tongueTwister.Remove(key); // Displays the Hashtable after deletion. Console.WriteLine("
Hashtable after removing the key = "{0}":",key); foreach (DictionaryEntry elem in tonguetwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); } }
在此程序中,我们有一个哈希表,其中包含绕口令"她在海边卖贝壳"。我们将键编号为 1a、1b、1c、2a、2b 等。首先,我们显示整个哈希表。然后我们使用 Remove() 方法并删除键 = 3b 的元素。再次显示新更新的哈希表。
输出
程序生成以下输出。
The Hashtable initially contains the following:
3b: shore
1a: She
1b: sells
2b: on
2c: the
3a: sea
2a: shells
1c: sea
Hashtable after removing the key = "3b":
1a: She
1b: sells
2b: on
2c: the
3a: sea
2a: shells
1c: sea
Note the hashtable after deleting the key = 3b.
Hashtable 类的 Remove() 方法用于一次从哈希表中移除或删除一个元素。如果指定的键(方法的参数)不在哈希表中,则 Remove() 方法不会引发异常。它将继续执行程序而不更改哈希表。
以上就是有关 Remove() 方法的全部内容,该方法用于根据指定的键从哈希表集合中删除项目。

