How to: Watch an Expression in the Debugger
The Watch window and QuickWatch dialog box are places where you can enter variable names and expressions that you want to watch during a debugging session.
The QuickWatch dialog box enables you to examine a single variable or expression at a time. It is useful for taking a quick look at one value or a larger data structure. The Watch window can store a number of variables and expressions that you want to view over the course of the debugging session. Some editions of Visual Studio have multiple Watch windows, which are numbered Watch1 through Watch4.
A variable name is the simplest expression you can enter. If you are debugging native code, you can use register names as well as variable names. The debugger can accept much more complex expressions than that, however. For example, you could enter the following expression to find the average value of three variables:
(var1 + var2 + var3) / 3
In C#, you can enter an object ID, or alias, instead the object name.
The debugger accepts most legal language expressions in a Watch window. For more information, see Expressions in the Debugger.
If you are programming in native code, you may sometimes need to qualify the context of a variable name or an expression containing a variable name. The context means the function, source file, and module where a variable is located. If you need to do this, you can use the context operator syntax. For more information, see Context Operator (C/C++ Language Expressions).
Expressions that Affect the State of Your Program
Evaluating some expressions can change the value of a variable or otherwise affect the state of your program. For example, evaluating the following expression changes the value of var1
:
var1 = var2
Expressions that change data are said to have side effects. If you enter an expression that has a side effect into the Watch window, the side effect will occur every time the expression is evaluated by the Watch window. This can produce unexpected results if you are unaware of the fact that the expression has side effects. An expression that is known to have side effects is only evaluated once, when you first enter it. Subsequent evaluations are disabled. You can manually override this behavior by clicking on a refresh icon that appears next to the value.
Unexpected side effects are often the result of function evaluation. For example, you could enter the following function call into the Watch window.
Func1(var1)
If you call a function from the Watch window or QuickWatch, the function you are calling might change data, creating a side effect. One way to avoid possible unexpected side effects from function evaluation is to turn automatic function evaluation off in the Options dialog box. This disables automatic evaluation of newer language features, such as properties, but it is safer.
Note |
---|
When you look at an expression in the Watch window, you might see a refresh icon, which looks like two green arrows, circling in opposite directions within a green circle. This is especially likely if you have turned automatic function evaluation off. The refresh icon indicates an error or out-of-date value. For more information, see How to: Refresh Watch Values. |
For native code and C#, you can specify a display format for the expression value using format specifiers. For more information, see Format Specifiers in C++ and Format Specifiers in C#.
The Visual Studio debugger automatically expands common data types to show their most important elements. You add expansions for custom data types. For more information, see Displaying Custom Data Types and Visualizers.
Note |
---|
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings. |
To evaluate an expression in the Watch window
In the Watch window, click an empty row in the Name column. The debugger must be in break mode at this point. Type or paste the variable name or expression you want to watch.
—or—
Drag a variable to a row in the Watch window.
Press ENTER.
The result appears in the Value column. If you type the name of an array or object variable, a tree control appears next to the name in the Name column. Expand or collapse the variable in the Name column.
The expression remains in the Watch window until you remove it.
To evaluate an expression in QuickWatch
In the QuickWatch dialog box, type or paste the variable, register, or expression into the Expression text box.
Click Reevaluate Or press ENTER.
The value appears in the Current value box.
If you type the name of an array or object variable in the Expression box, a tree control appears next to the name in the Current value box. Expand or collapse the variable in the Name column.
To reevaluate a previous expression in QuickWatch
In the QuickWatch dialog box, click the down arrow that appears at the right of the Expression box.
Choose one of the previous expressions from the drop-down list.
Click Reevaluate.
See Also
Tasks
How to: Use Debugger Variable Windows