从C#中的哈希表中删除项目
以下是我们的哈希表-
Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");
若要删除项目,请使用Remove()方法。在这里,我们要删除第三个元素。
h.Remove(3);
让我们看完整的例子。
示例
using System;
using System.Collections;
public class Demo {
public static void Main() {
Hashtable h = new Hashtable();
h.Add(1, "Jack");
h.Add(2, "Henry");
h.Add(3, "Ben");
h.Add(4, "Chris");
Console.WriteLine("初始列表:");
foreach (var key in h.Keys ) {
Console.WriteLine("Key = {0}, Value = {1}",key , h[key]);
}
//删除项目
h.Remove(3);
Console.WriteLine("New list after删除项目: ");
foreach (var key in h.Keys ) {
Console.WriteLine("Key = {0}, Value = {1}",key , h[key]);
}
}
}输出结果
初始列表: Key = 4, Value = Chris Key = 3, Value = Ben Key = 2, Value = Henry Key = 1, Value = Jack New list after删除项目: Key = 4, Value = Chris Key = 2, Value = Henry Key = 1, Value = Jack
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短