继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

学习Key与Value的集合hashtable

慕婉清0083722
关注TA
已关注
手记 322
粉丝 72
获赞 279

你可以创建一个hashtable:

 

你可以使用foreach方法,把hashtable的key与value循环写出来:

 

在控制台屏幕输出:

 

如果只需把key输出:

 

如果只想把值循环输出:

 

测试输出结果:

 

 

往hashtable集合添加key与value:

 

有添加就是移除:

 

测试上面的添加Add和移除:

 

key值为"A"已经被移除。

 

接下来,再练习2个方法,就是判断key或avlue是否已经存在集合:

完整练习代码:

 class Av    {        public Hashtable HashtableEntity = new Hashtable()        {            { "A", "Leo" },            { "B", "Insus.NET" },            { "C", "Jack" }        };        public void Output()        {            foreach (DictionaryEntry de in HashtableEntity)            {                Console.WriteLine(string.Format("Key: {0}; Value: {1}", de.Key, de.Value));            }        }                public void ForeachKey()        {            foreach (string key in HashtableEntity.Keys)            {                Console.WriteLine(key);            }        }        public void ForeachValue()        {            foreach (string value in HashtableEntity.Values)            {                Console.WriteLine(value);            }        }                public void Add(string key, string value)        {            HashtableEntity.Add(key, value);        }        public void Remove(string key)        {            HashtableEntity.Remove(key);        }        public bool isExitByKey(string key)        {            return HashtableEntity.ContainsKey(key);        }        public bool isExistByValue(string value)        {            return HashtableEntity.ContainsValue(value);        }    }

Source Code

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP