zoukankan      html  css  js  c++  java
  • Dictionary(一)

     // <summary>     
            /// 一般用法  
            /// </summary>
            public static void DictionaryDemo001()
            {
                Dictionary<int, string> dict = new Dictionary<int, string>();
                dict.Add(1, "111");
                dict.Add(2, "222");
                //判断是否存在相应的key并显示    
                if (dict.ContainsKey(2))
                { Console.WriteLine(dict[2]); }
                //遍历Keys         
                foreach (var item in dict.Keys)
                { Console.WriteLine("Key:{0}", item); }
                //遍历Values       
                foreach (var item in dict.Values)
                { Console.WriteLine("value:{0}", item); }
                //遍历整个字典       
                foreach (var item in dict)
                { Console.WriteLine("key:{0} value:{1}", item.Key, item.Value); }
            }
            /// <summary>      
            /// 参数为其它类型      
            /// </summary>      
            public static void DictionaryDemo002()
            {
                Dictionary<string, string[]> dict = new Dictionary<string, string[]>();
                dict.Add("1", "1,11,111".Split(','));
                dict.Add("2", "2,22,222".Split(','));
                Console.WriteLine(dict["2"][2]);

            }
            public static void DictionaryDemo003()
            {
                IDictionary<int, yongfa365> dict = new Dictionary<int, yongfa365>();
                for (int i = 0; i < 10; i++)
                {
                    yongfa365 y = new yongfa365();
                    y.UserCode = i;
                    y.UserName = "www.yongfa365.com " + i.ToString();
                    dict.Add(i, y);
                }

                foreach (var item in dict)
                {
                    Console.WriteLine("{0} One:{1} UserName:{2}", item.Key, item.Value.UserCode, item.Value.UserName);

                }
            }

        }

        public class yongfa365
        {
            public int UserCode { get; set; }
            public string UserName { get; set; }
        }

    *****************************************************************************

      public IDictionary<string, string> GetDic(string xml)
             {
                 IDictionary<string, string> dit = new Dictionary<string, string>();
                 string[] arrays = xml.Split(';');
                 foreach (string item in arrays)
                 {
                     string[] param = item.Split(':');
                     if (param[0] != null && param[1] != null)//注意:必须key值和Value值都存在才可加入
                     {
                         dit.Add(param[0], param[1]);
                     }
                 }
                 return dit;
             }

             public User GetUser(IDictionary<string, string> dit)
             {
                 user = new User();
                 credit = new UserCredit();

                 IEnumerator<KeyValuePair<string, string>> dem = dit.GetEnumerator();
                 while (dem.MoveNext())
                 {
                     string name = dem.Current.Key;
                     string value = dem.Current.Value;
                     switch (name)
                     {
                         case "user_id": user.UserId = Convert.ToInt64(value); break;
                         case "type": user.Type = value; break;
                         case "sex": user.Sex = value; break;
                         case "nick": user.Nick = value; break;
                         case "created": user.Created = value; break;
                         case "total_num": credit.TotalNum = Convert.ToInt64(value); break;
                         case "score": credit.Score = Convert.ToInt64(value); break;
                         default: break;
                     }
                 }
                 return user;
             }

             public User GETUser(IDictionary<string, string> dit)
             {
                 user = new User();
                 credit = new UserCredit();
                 Dictionary<string, string> dic = new Dictionary<string, string>(dit.Count);
                 foreach (KeyValuePair<string, string> kvp in dic)
                 {
                     string name = kvp.Key;
                     switch (name)
                     {
                         case "type": user.Type = kvp.Value ; break;
                         case "sex": user.Sex = kvp.Value; break;
                         case "nick": user.Nick = kvp.Value; break;
                         case "created": user.Created =kvp.Value; break;
                         default: break;
                     }
                 }
                 return user;
             }

             public string  Test(IDictionary<string, string> dit)
             {
                 string KV = null;
                 Dictionary<string, string> dic = new Dictionary<string, string>(dit.Count);
                 foreach (var item in dic)//注意:值为空的情况
                 {
                     KV +="键:"+ item.Key + "值:"+item.Value;
                 }
                 //foreach (var value in dic.Values)
                 //{
                 //    KV += "值的集合:" + value;
                 //}
                return KV;
             }

  • 相关阅读:
    【资料】linux进程间通信的方式
    [资料]二叉树各种遍历方法
    【提问】有关short和int溢出的一个有趣测试
    【总结】简单易用的linux命令行清单
    沐风心扬C#编程速查系列之Web编程专题(1)获取本机IPV4地址
    Vmware6.5给Linux虚拟机增加空间
    【已解决】一段使用宏定义求结构体偏移量的C代码
    【疑问】有关C语言中printf函数的输出和格式的问题
    Sql2008 View sys.sysobjects Angkor:
    XMLHttpRequest Angkor:
  • 原文地址:https://www.cnblogs.com/SanMaoSpace/p/2147983.html
Copyright © 2011-2022 走看看