How to know WHAT or WHO accesses a variable in VS Debugger for C#?

141 views Asked by At

I am debugging an existing C# program. There are of course events and event handlers etc.. My problem is I don't know where and when in the program this specific variable named X for the purpose of the question is being updated.

When we worked with emulators and microprocessors, I could see what code accesses a specific address in the memory but I am not sure if I could see the same thing in C# debugger.

3

There are 3 answers

1
Shahzad Qureshi On

Some options:

  1. Right click on the variable and select "Find All References"...

  2. Do a global solution search-all for the text "x =" and "x=" to find everywhere something is trying to set it.

1
code4life On

I'm not totally sure if you mean "what potential points in my code might be able to reach this variable?", or if you mean, "I have a breakpoint on this variable but have no idea how it got here?".

If it's question #2, I'd suggest looking at the call stack (Debug/Windows). Also, if you have multiple thread or tasks in the mix, make sure to View All Threads (same Debug menu) and View All Tasks.

Hope that helps!

0
Stas Sh On

You cant really do this for objects fields, but you can do it for Properties (and you can usually change the code and wrap a field in a property without making changes in other code!).

I have answered a similar questions on how you can set a breakpoing on the generated setter method here, by using Oz Code:

Reasking about hitting breakpoint at property setter