Setting Your Watch

When you’re debugging a program with many local variables, you usually don’t want to watch all of them; you need to keep track of only a few. You can track specific variables and objects in the Watch window. You can have up to four Watch windows at a time in Visual Studio, but just the one in C# Express. Watch windows are like by-invitation versions of the Locals window; they list the objects you ask the debugger to keep an eye on, and you can see their values change as you step through the program, as illustrated in Figure 9-12.

A Watch window lets you keep an eye on just the variables you want to track.

Figure 9-12. A Watch window lets you keep an eye on just the variables you want to track.

The Watch windows are usually tabbed with the Locals window. You can add a watch by right-clicking on a variable and choosing Add Watch or you can just drag the variable to the Watch window. The variable will be added to your Watch window. To remove a variable that you’ve added to your Watch window, you can right-click on it in the Watch list and select Delete Watch.

In Visual Studio only, if you just need to peek at a variable, and perhaps to experiment with manipulating its value, you can right-click on it and choose QuickWatch, which opens a dialog box with watch information about a single object, as shown in Figure 9-13.

You can enter any expression into the Expression field and evaluate it from within the QuickWatch window. For example, suppose you ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.