C#Dictionary的使用问题

stringsDictionaryKey="001";//string[]sDictionaryValue={"张三","32","经理","8000"};string[... string sDictionaryKey = "001";
//string[] sDictionaryValue = { "张三", "32", "经理", "8000" };
string[] sDictionaryValue = new string[4];
sDictionaryValue[0] = "张三";
sDictionaryValue[1] = "32";
sDictionaryValue[2] = "经理";
sDictionaryValue[3] = "8000";
myDictionary.Clear();
myDictionary.Add(sDictionaryKey, sDictionaryValue);

sDictionaryKey = "002";
sDictionaryValue[0] = "李四";
sDictionaryValue[1] = "23";
sDictionaryValue[2] = "主管";
sDictionaryValue[3] = "6000";
myDictionary.Add(sDictionaryKey, sDictionaryValue);

以上代码执行后会出现001和002的value一样???为什么呢?
展开
 我来答
freeeeeewind
推荐于2016-01-20 · TA获得超过1万个赞
知道大有可为答主
回答量:3227
采纳率:94%
帮助的人:1342万
展开全部
估计myDictionary 是这样定义的:
Dictionary<string, string[]> myDictionary = new Dictionary<string, string[]>();

当你第一次对 string[]赋值(张三,32……)后并存放到myDictionary中键为"001",存入myDictionary的“键——值”对为: "001" ——sDictionaryValue
接着你有将sDictionaryValue重新赋值,添加到集合后“键——值”对为: "002" ——sDictionaryValue
这里要注意:sDictionaryValue此时已经是(李四,23……)了
追问
难度真是引用参数的问题??我想你因该知道我想要的效果吧?求求你帮帮我吧....
追答
string sDictionaryKey = "001";
//string[] sDictionaryValue = { "张三", "32", "经理", "8000" };
string[] sDictionaryValue = new string[4];
sDictionaryValue[0] = "张三";
sDictionaryValue[1] = "32";
sDictionaryValue[2] = "经理";
sDictionaryValue[3] = "8000";
myDictionary.Clear();
myDictionary.Add(sDictionaryKey, sDictionaryValue);

string[] sDictionaryValue2 = new string[4];
sDictionaryKey = "002";
sDictionaryValue2[0] = "李四";
sDictionaryValue2[1] = "23";
sDictionaryValue2[2] = "主管";
sDictionaryValue2[3] = "6000";
myDictionary.Add(sDictionaryKey, sDictionaryValue2);
匿名用户
2014-04-02
展开全部
数组是引用类型
更多追问追答
追问
不是吧?那局部变量释放后它不就没值了? 我要怎么才能到达我要的效果呢?
追答
            string sDictionaryKey = "001";
            string[] sDictionaryValue = new string[4];
            sDictionaryValue[0] = "张三";
            sDictionaryValue[1] = "32";
            sDictionaryValue[2] = "经理";
            sDictionaryValue[3] = "8000";
            myDictionary.Add(sDictionaryKey, sDictionaryValue);

            //重新new一个数组
            sDictionaryValue = new string[4];
            sDictionaryKey = "002";
            sDictionaryValue[0] = "李四";
            sDictionaryValue[1] = "23";
            sDictionaryValue[2] = "主管";
            sDictionaryValue[3] = "6000";
            myDictionary.Add(sDictionaryKey, sDictionaryValue);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式