如何更新字典值; [英] How to update dictionary value;

查看:215
本文介绍了如何更新字典值;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

Dictionary<int, myClass> ActiveTabIndex = new Dictionary<int, myClass>();




public class myClass
    {
        public int TabIndex { get; set; }
        public string OdSessionId { get; set; }
        public string TextBoxValue { get; set; }
    }





i我正在为字典添加值



i am adding value to dictionary like

ActiveTabIndex.Add(1, new myClass{ TabIndex = 1, OdSessionId = "S", TextBoxValue=""});





现在我想要为Key 1更新TextBoxValue,

喜欢



now i want to update TextBoxValue for Key 1,
like

ActiveTabIndex[1] = new myClass{ TextBoxValue="something"};





但是将其他值重置为默认值,

如何更新特定键的字典值而不用覆盖其他价值。



我尝试过:





but its Resting the other value to default,
How Can I update The value of dictionary for the specific key with out overriding Other value.

What I have tried:

ActiveTabIndex[1] = new myClass{ TextBoxValue="something"};



但是正在更新其他值toi default


But Is Updating The Other value toi default

推荐答案

因为您正在创建一个具有新状态的新对象,但您只需要更新当前对象的状态。



你需要这样做:



Because you are creating a new object with new state but you need to just update the state of current object.

You need to do it this way :

ActiveTabIndex[1].TextBoxValue="something";





现在你将只保留其他属性值更新的实例的 TextBoxValue 属性。



希望它能帮助你理解。



Now you will have just TextBoxValue property of the instance updated with the other properties value retained.

Hope it helps you understand.


这篇关于如何更新字典值;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆