Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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()


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?
Posted

If you want to check for if two variables point to the same object instance, use Object.ReferenceEquals()[^].
 
Share this answer
 
The .Equals(object ...) method will do what you need:

C++
foo.Equals(bar)


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 :-).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900