如何在C#中更新字典值; [英] How to update dictionary value in C#;

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

问题描述

I have

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; }
    }

我是向字典添加值,例如 

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

现在我想更新TextBoxValue键1, 
喜欢

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

但它的其余部分值为默认值, 

=   

11111 1; font-family:'Segoe UI',Arial,sans-serif; font-size:14px">如何更新特定键的字典值而不覆盖其他值。


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.





推荐答案

将其创建为静态。但是将字典公开为公共使用它应该是错误的。使用封装原则来在字典中添加和获取值。这意味着你将实现public static void Add(myClass myClassObject)方法和public
static myClass GetmyClassObjectByKey(int key)方法来处理字典。
Create it as static. But it should be wrong to expose dictionary as public to work with it. Use encapsulation princip to Add and Get values to/from dictionary. It means you will implement public static void Add(myClass myClassObject) method and public static myClass GetmyClassObjectByKey(int key) method to work with dictionary.


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

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