键值对拼接的妙用

参考自百科的这篇文章,相信聪明的你一定能够看懂。

 var sb = new StringBuilder();
            var dict = new Dictionary<string, string>();
            dict.Add("1","a");
            dict.Add("2", "b");
            dict.Add("3", "c");
            dict.Add("4", "d");
            dict.Add("5", "e");
            foreach (var kv in dict) 
                sb.AppendFormat("{0}={1}&", kv.Key, kv.Value);
            string str = sb.ToString();
            string stt = str.Remove(str.Length-1);

  优化后:

 var sb = new StringBuilder();
            var dict = new Dictionary<string, string> {{"1", "a"}, {"2", "b"}, {"3", "c"}, {"4", "d"}, {"5", "e"}};
            foreach (var kv in dict) 
                sb.AppendFormat("{0}={1}&", kv.Key, kv.Value);
            string str = sb.ToString();
            string stt = str.Remove(str.Length-1);

  我的c#能力在以看得见的速度增长着,codding之余更多了一份快乐和自信。可是,改变了我的生命中的那座城中的她,去了哪里,我该怎样才能找到她。

posted on 2014-07-24 22:59  鸣动我心  阅读(347)  评论(0编辑  收藏  举报