Reputation: 9825
in what way can I output the reference of an object in memory. Like:
Console.WriteLine("Object at Memory Location " + object.Reference);
thanks in advance, I need this to do some debugging.
Upvotes: 1
Views: 344
Reputation: 54017
Every so often you may see a .NET tutorial mention "pointers" and someone get mad in the comments saying that "references" and not "pointers" should be discussed. This is why. The distinction is usually trivial, especially when teaching higher concepts but at times like this, it's very non-trivial.
The actual pointers are handled by .NET and .NET often changes the locations of objects in memory and thus changes/updates the pointers. You and I deal with the references and our reference doesn't need to change when the pointers do because .NET handles this mapping for us. So although there are always pointers involved, even if we do get what the pointer is pointing to at the moment, this doesn't necessarily mean that you're finding out where it will continue to be.
That said, you can still use the below, but I don't think it will achieve what you desire:
int* myIntPointer = &myInt;
Upvotes: 2
Reputation: 11243
For a managed object you can't and for good reason. It can change location during any garbage collection.
Upvotes: 4