开发者

Is there any way to expose pointer addresses in C#?

I have a confusing problem in my code, in which one object gets initialized properly, but when I look at it thro开发者_Python百科ugh a parent reference, its properties arent initialized anymore. I assigned id's for my objects, and I am perfectly sure that there are no duplicate objects lying around. When looking through the parent, the child is not initialized, but This is something I don't understand. Shoudn't C# references point to the same memory space, and therefore show the correct values even when another reference changed something in that memory space?

EDIT: I don't need the problem above solved. Just a reminder that this is the question which I'm interested in:

So back to the headline question: For easier debugging, I would like to look behind all those abstracted references like I could do in C++. I think it would be immediately clear to me what went wrong when I could do that.


Is it possible that you're using structs and not classes? They get copied on assignment, so changing (or initializing) does not affect the copied object.

If you just want to check whether your objects are the same you can just compare their references. Getting a pointer/address in memory is not possible in a "safe" environment.


If you want to keep track of which object a particular reference is pointing at while debugging, then you can right click on a reference in a Watch window and select 'Make Object ID'. Any other references to the same object will now show the same ID in the debugger (e.g. {1#}), and you can add the ID as a value to watch even if there are no references on the current stack frame.

See here for an example


you can compare references... sounds like you are copying an object somewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜