Directionary字典键值对查找

    xiaoxiao2021-03-25  119

    using System; using System.Collections.Generic;

    class test {     static void Main()     {         Dictionary<string, int> dic = new Dictionary<string, int>();         dic.Add("zhangsan", 1101);         dic.Add("lisi", 1102);         dic.Add("wangwu", 1103);         dic.Add("zhaoliu", 1104);

            foreach (string str1 in dic.Keys)         {             if (dic[str1].Equals(1103))             {                 Console.WriteLine("恭喜你!,找到了,1103对应的姓名是{0}", str1);             }         }

            foreach (string str in dic.Keys)         {              Console.WriteLine("对应的姓名是{0}:{1}",str, dic[str]);         }

        }

       1、以键找值直接Value=dic[str]

    2、以值找键则可以foreach(string str in dic.Keys)

                                  {  

                       if(dic[str].Equal(Value);)                        {     Console.WriteLine(str);   }

                                  }

    转载请注明原文地址: https://ju.6miu.com/read-6436.html

    最新回复(0)