vii Ways to Look at the Values of Variables While Debugging in Visual Studio

When you are running your code and something is behaving unexpectedly, how exercise you discover out what is going wrong? When I was in school the starting time way I learned how to debug a wonky application was by sticking "print()" statements all over the place, running my code, and looking back through the log of output seeing if I noticed that something looked incorrect. Then if I wanted to expect at the value of another variable I would accept to add a new "impress()" statement, recompile, and re-run the application again. This can exist a tiresome process, so I am pleased to tell you there is a better way than littering your lawmaking with a slew of "print()" statements y'all'll take to remove later. You can utilise the tools of the Visual Studio debugger to inspect variables on the fly.

In this post I will review 7 different ways to audit the values of your variables while debugging through your code in the Visual Studio debugger without modifying your code. Which means do you already use? Permit united states know in the comments below.

If you're interested in giving united states feedback well-nigh our future ideas, please help us improve the Visual Studio debugger past joining our community.

In social club to await at the value of variables while you are debugging, yous first need to be in break style. Y'all can be stopped at a breakpoint, stopped while stepping, or even stopped at an Exception, and so you will take access to your variable values using these techniques.

1. DataTip

Hover over a variable to see its value.

The well-nigh normally used way to wait at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable yous want to look at. The DataTip will announced showing you the value of that variable. If the variable is an object, you tin can expand the object by clicking on the arrow to see the elements of that object.

DataTip1

You can also "pin" a DataTip to your lawmaking by clicking on the pin icon next to a value. This mode you tin see the value modify correct in-line with your code as you are stepping through it. This can come up in handy when debugging within of loops where you are watching a unmarried value continually change.

PinnedDataTip

2. Autos Window

**See variables used well-nigh your teaching arrow. **

As you stop at unlike places in your code the Autos window will automatically populate with all the variables used in the current argument (the line where the yellow didactics pointer is) and some surrounding statements. The scope of what variables automatically populate from the surrounding statements depends on the linguistic communication you are debugging. Typically, you will meet variables that are referenced 3 lines behind and 1 line ahead of the current argument. Some languages like JavaScript are non supported past the Autos window.

image

Open upwards this window from Debug/Windows/Autos Window (Ctrl+Alt+V, A)

3. Locals Window

**Come across variables that exist in the local telescopic of your electric current stack frame. **

The Locals window volition populate with the local variables for the electric current method that accept. Like the Autos window, variables that appear here are automatically populated. Deciding which window you adopt to use depends on which telescopic of the variables you lot would similar to encounter.

In all of our variable windows, when whatsoever values of the variables change the new values will appear in red text.

image

Open up upwards this window from Debug/Windows/Locals Window (Ctrl+Alt+Five, L).

4. Watch Windows

Evaluate variables and **expressions and keep track of the results. **

In that location are 4 Watch windows available to employ. These windows commencement out blank and permit you add the names of variables that you care nigh watching equally y'all debug your application. You can click in the editable line under the name cavalcade and blazon in the variable name whose value yous want to come across. Or you tin can right click on variables from your code editor and select "Add to Scout".

Watch

Beyond adding just a single variable, you can blazon in by and large any expression. Then yous can potentially call methods, evaluate lambdas, or just do things like "i+3". Note that when you lot blazon in an expression that executes code there could be side furnishings that change the state of your application.

image

Open up upward these windows from Debug/Windows/Lookout (Ctrl+Alt+West, one).

five. QuickWatch dialog

**Apace audit the value and properties of a unmarried variable or expression in a disposable pop-upwards diag. **

The QuickWatch window is a dialog that you can use to inspect a single variable or expression. This temporary dialog is convenient to use when y'all desire more infinite to inspect all of the backdrop of an object, but don't want to modify your window layout.

image

Open the QuickWatch window by right clicking on a variable and selecting "QuickWatch…" or by using the keyboard shortcut Shift+F9.

vi. Parallel Sentry Windows

Run across the value of a variable or expression simultaneously beyond multiple threads or recursive calls.

There are four Parallel Watch windows available to use. Similar to the Watch windows, you lot can click in the editable line and type in the variable proper name or expression whose value yous desire to see.

Each row in the window identifies a unlike thread and and then you tin run across the value of the same variable across all your electric current threads.

ParallelThread

It will also show you the value of a variable in a recursive stack across the aforementioned thread, where each row is an iteration of that function call.

ParallelRec

Open these windows from Debug/Windows/Parallel Watch (Ctrl+Shift+D, ane).

Use this scratch pad to view and change the values of variables at a point in time.

While stopped at a line of code, you can type a variable or expression into the Immediate window and hit enter to view the result. The Immediate window evaluates expressions just like the Watch windows and tin can result in side furnishings.

Immediate

You can also use the Immediate window to execute complex expressions (even evaluate lambda expressions) and view the results without having to edit and re-execute your code. This window can also be used at blueprint time, earlier you accept started a debugging session.

image

Open the Immediate Window from Debug/Windows/Immediate Window (Ctrl+Alt+I).

Wrap Up

This list provides a basic overview of different ways to inspect variables using Visual Studio. What is your favorite fashion to see your variable values while debugging? Let us know in the comments below.

If yous are interested in helping give feedback about our future ideas, sign up to help us better the Visual Studio Debugger.