判断 Dictionary 中的 key 是否存在
ITPOW2018/9/12 14:47:33
如果 key 是对象,则要注意,因为对象是按地址匹配的:
C c1 = new C(); c1.Name = "a"; C c2 = new C(); c2.Name = "a"; Dictionary<C, string> dic = new Dictionary<C, string>(); dic.Add(c1, "A"); Response.Write(dic[c1]); // 给定关键字不在字典中。
如果 key 是结构,则是按值匹配的:
Dictionary<KeyValuePair<int, int>, string> dic = new Dictionary<KeyValuePair<int, int>, string>(); dic.Add(new KeyValuePair<int, int>(1, 2), "a"); Response.Write(dic[new KeyValuePair<int, int>(1, 2)]); // 显示 a
<<返回首页<<