首页 新闻 会员 周边

新手求C# Dictionary的ContainsValue方法示例代码

0
[已解决问题] 解决于 2016-09-09 21:00

刚刚学习C#不久,对好多东西不懂,求高手指点

梦天涯的主页 梦天涯 | 初学一级 | 园豆:103
提问于:2016-09-08 18:37
< >
分享
最佳答案
0

C# Dictionary的使用实例代码

class Dirctonary
    {
        public void DictionaryGet()
        {
            Dictionary<int, string> productList = new System.Collections.Generic.Dictionary<int, string>();
            productList.Add(1, "ProductionOne");
            productList.Add(2, "ProductionTwo");
            foreach (KeyValuePair<int, string> production in productList)
            {
                MessageBox.Show(string.Format("{0},{1}", production.Key, production.Value));
            } 
            //MessageBox.Show(productList.Count.ToString());
            //MessageBox.Show(productList[1].ToString());
            Dictionary<int, string>.KeyCollection keys = productList.Keys;
            foreach (var item in keys)
            {
                MessageBox.Show(item.ToString());
            }
            Dictionary<int, string>.ValueCollection collection = productList.Values;
            foreach (var item in collection)
            {
                MessageBox.Show(string.Format("{0}", item));
            }
            //productList.Remove(1);
            //productList.Clear();
            MessageBox.Show("判断是否包含键值对中的键为”1“的值");
            if (productList.ContainsKey(1))
            {
                MessageBox.Show(productList[1]);
            }
            MessageBox.Show("判断是否包含键值对中的值为”ProductionTwo“的值");
            if (productList.ContainsValue("ProductionTwo"))
            {
                MessageBox.Show(string.Format("{0}", "this really exists"));
            }
        }

 

奖励园豆:5
雨之秋水 | 小虾三级 |园豆:649 | 2016-09-08 19:01
其他回答(2)
0

微软起名字是一绝,所以你看名字就知道,这个方法是:判断是否包含某个值。。。

顾晓北 | 园豆:10844 (专家六级) | 2016-09-09 09:03
0

msdn上面有很详细的说明

https://msdn.microsoft.com/zh-cn/library/xfhwa508(v=vs.110).aspx

Dictionary<TKey, TValue> 类

System_CAPS_pubmethod ContainsKey(TKey)

确定是否 Dictionary<TKey, TValue> 包含指定键。

也就是 是否保存 TKey

本文由机器翻译。若要查看英语原文,请勾选“英语”复选框。 也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语

Dictionary<TKey, TValue> 类

凝冰 | 园豆:685 (小虾三级) | 2016-09-09 17:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册