Dictionary<(Of <(TKey, TValue>)>) 当TKey是类时,如何让ContainerKey正确?

msys200 2009-11-10 11:06:21

class Program
{
static void Main(string[] args)
{
Dictionary<A, string> dic = new Dictionary<A, string>();
A ta = new A();
ta.a = "a";
dic[ta] = "b";
A test = new A();
test.a = "a";
Console.WriteLine(dic.ContainsKey(test)); //这里输出的是false
}
}

public class A
{
public string a;
}
...全文
43 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljhcy99 2009-11-10
  • 打赏
  • 举报
回复
这样呢?
Dictionary<A, string> dic = new Dictionary<A, string>();
A ta = new A();
ta.a = "a";
dic[ta] = "b";
A test = ta;
Console.WriteLine(dic.ContainsKey(test)); //这里输出的是false
wartim 2009-11-10
  • 打赏
  • 举报
回复
Console.WriteLine(dic.ContainsKey(ta));
ta 和 test 已经不是同一个了
zhujiazhi 2009-11-10
  • 打赏
  • 举报
回复
http://social.microsoft.com/Forums/es-ES/visualcshartzhchs/thread/de37d6b2-4ee4-4029-8a6a-99ff2c0cdc67
msys200 2009-11-10
  • 打赏
  • 举报
回复
我想要输出时true的

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧