C#实现Hashtable中
根据键值对的值,然后进行删除这一个键值对
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace Test1112 { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("1","张三"); ht.Add("2", "李四"); ht.Add("3", "王五"); ht.Add("4", "小明"); if(ht.ContainsValue("张三")){ Console.WriteLine("学生张三已经在集合中了!"); }else{ ht.Add("5","小刚"); } ht.Remove("2"); foreach(string key in ht.Keys) { Console.WriteLine(key +":"+ht[key]); } Console.WriteLine("---------------------------------"); foreach(DictionaryEntry de in ht) //fileht为一个Hashtable实例 { //Console.WriteLine("aaaaa :" + de.Key.GetType());//de.Key对应于keyvalue键值对key,System.String //Console.WriteLine("bbbbbbbb :" + de.Value.GetType());//de.Key对应于keyvalue键值对value,System.String string a = de.Value.ToString(); //Console.WriteLine(a); string b = "张三"; //已经是 if(a.Equals(b)) { //Console.WriteLine(de.Key.GetType()); object c = de.Key;//因为ht.Remove(object o)里面只能传入object类型,所以这边进行隐式转换 ht.Remove(c); break;//找到了直接让循环结束 } } //显示结果 Console.WriteLine("--------------------------------"); Console.WriteLine("最后的输出结果为:"); foreach (string key in ht.Keys) { Console.WriteLine(key + ":" + ht[key]); } Console.Read(); } } }