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一样???为什么呢? 展开
//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一样???为什么呢? 展开
2个回答
展开全部
估计myDictionary 是这样定义的:
Dictionary<string, string[]> myDictionary = new Dictionary<string, string[]>();
当你第一次对 string[]赋值(张三,32……)后并存放到myDictionary中键为"001",存入myDictionary的“键——值”对为: "001" ——sDictionaryValue
接着你有将sDictionaryValue重新赋值,添加到集合后“键——值”对为: "002" ——sDictionaryValue
这里要注意:sDictionaryValue此时已经是(李四,23……)了
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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询