日期:2014-05-17  浏览次数:20989 次

如何更新Dictionary内的value?
本帖最后由 qiuqingpo 于 2010-09-29 09:10:20 编辑

using System;
using System.Collections.Generic;
using System.Text;

namespace Client
{
    class Test
    {
        private Dictionary <string,int> userlist=new Dictionary<string,int>() ;
        public Test()
        {
            userlist.Add("qiu", 178);
            if (userlist.ContainsKey("qiu"))
            {
                //如何更新这个key值为qiu的value.有没有好的方法?
                //Console.WriteLine("成功");
            }
        }
    }
}


------解决方案--------------------
userlist["qiu"]=***
------解决方案--------------------
userlist.Add("qiu", 444);

------解决方案--------------------
 Dictionary <string,int> userlist=new Dictionary<string,int>() ;

                 foreach (KeyValuePair<string, int> item in userlist)
                 {
                     if(item.Key =="qiu") 
                     {
                           item.Value='newValue';
                     }
                 }
------解决方案--------------------
引用:
引用:

userlist["qiu"]=***

试了一下确实不错.不过我还想知道有没有别的好方法.谢谢


在NET里面,我的映像中好像没有提供其它方法来赋值,就一楼的,难道不满足你吗?
------解决方案--------------------
上面的方法简单明了 还不可以吗 你想实现什么啊
------解决方案--------------------
两种方法供你使用,LS已经有正解了!我整理一下

 class Test
    {
        private Dictionary<string, int> userList = new Dictionary<string, int>();
        public Test()
        {
            //方法一
            userList.Add("学生", 1);