检查两个引用是否指向VS2k8调试器中的同一对象 [英] Check if two references are pointing to the same object in the VS2k8 debugger

查看:99
本文介绍了检查两个引用是否指向VS2k8调试器中的同一对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我花了大约6个小时来寻找一个错误,该错误是由两个变量引用同一个对象引起的,而我需要的是让第二个变量拥有自己的第一个副本:

I''ve lost about 6 hours over the last few days tracking down a bug that was caused by having two variables reference the same object when what I needed was to have the second one have its own copy of the first:

    MyObjectType foo = new MyObjectType()

// lots of intervening code   

    MyObjectType bar = foo;  //should have been = foo.Clone()



在C/C ++中,这种错误很容易在调试器中确认,因为我可以检查存储的两个指针的地址.不过,我从未能够找到.net的简单等效项.



In C/C++ this sort of error was easy to confirm in the debugger since I could just check was address the two pointers stored. I''ve never been able to find a simple equivalent for .net though; am I blind or is this an oversight on MS''s part?

推荐答案

如果要检查两个变量是否指向同一对象实例,请使用 Object.ReferenceEquals() [
If you want to check for if two variables point to the same object instance, use Object.ReferenceEquals()[^].


.Equals(object ...)方法将满足您的需求:

The .Equals(object ...) method will do what you need:

foo.Equals(bar)



它将参考与参考类型进行比较.请参阅:
http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx [ ^ ]了解更多信息.

[自行编辑]:决定开始以可识别的英语语言进行写作,这样,看着我的答案的人实际上就可以阅读它了:-).



It compares the references for reference types. See: http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx[^] for more info.

[Self-edit]: Decided to start writing in a language that is recognizably English, that way people looking at my answer might actually be able to read it :-).


这篇关于检查两个引用是否指向VS2k8调试器中的同一对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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