Visual Studio's Watch: track C# code (2023)

During debugging we can explore variables and expressions at different points in a C# program. But what really helps to locate bugs is to see how they change while the program runs. Let’s see how Visual Studio’s ‘Watch’ window helps with that.

IN THIS ARTICLE:

# Debug in Visual Studio and track C# code with ‘Watch’

When we write C# applications, we at some point we run into a programming bug. For a basic program scanning the code is often enough to find the mistake. But for complex programs that isn’t doable. Luckily, we have a good alternative: debugging.

Debugging is the process of identifying and removing errors from our application (Asad & Ali, 2017). For that we often use special tools that help manage the complexities of analysing programming code.

To use debugging tools we first need to start our C# application under a debugger. In Visual Studio we do that with F5 (‘Start Debugging’;Visual Studio's Watch: track C# code (1)), F11 (‘Step Into’;Visual Studio's Watch: track C# code (2)), or the ‘Run To Cursor’ option.

Then when our program pauses (or so-called breaks) under the debugger, we can analyse the state of the program at that particular point (Liberty & MacDonald, 2009). We might for instance inspect the current values of variables or see what an expression evaluates to.

One powerful debugging feature is to step through the code. This executes the program line by line, and we can see in detail how it behaves with each step (Asad & Ali, 2017; Dorman, 2010). But there’s often a lot of code that changes, and we aren’t interested in everything.

Luckily, Visual Studio’s ‘Watch’ window shows us what the values of variables and expressions are at each debugging step (Stephens, 2014). With this helpful debugging tool we can track (or watch) specific expressions we’re interested in.

So if we suspect that some variables and expressions contribute to the programming bug, we’ll add them to the ‘Watch’ window. Then when we step through the code while debugging, we’ll see how their values change with each line of code executed.

Let’s see how ‘Watch’ helps us track variables, validate ideas, and test possible bug solutions all while debugging.

# Use the ‘Watch’ debugging window in Visual Studio

Visual Studio’s ‘Watch’ window is only available when we’re debugging code. And so we’ll first need to start debugging in Visual Studio. Ways to do that are to place a breakpoint or use the ‘Run To Cursor’ option.

Then once our program pauses under the debugger, look for the ‘Watch’ window that looks like:

Visual Studio's Watch: track C# code (3)

By the way, values in the ‘Watch’ window remain there until we remove them. So your ‘Watch’ window isn’t necessarily empty like the one shown here.

Cannot see the ‘Watch’ window in Visual Studio’s debugging mode? To open it, click on the ‘Debug’ menu item in Visual Studio, point to ‘Windows’, highlight ‘Watch’, and then select ‘Watch 1’ (or any of the other watch windows). An alternative is the Ctrl + Alt + W, 1 key combination.

Visual Studio's Watch: track C# code (4)

The four different ‘Watch’ windows (‘Watch 1’, ‘Watch 2’, ‘Watch 3’, and ‘Watch 4’) all work in the same way – there are just multiple of them so we can use a certain window for a specific purpose.

In this article we simply speak of the ‘Watch’ window, but know that this can refer to any of those four identical windows.

# Add variables and expressions to the ‘Watch’ window

To evaluate and track an expression in the ‘Watch’ window we first need to add that expression. When our program paused under the debugger, there are several ways to do that.

The first approach is a right-click on a code element in the editor and then choose ‘Add Watch’:

Visual Studio's Watch: track C# code (5)

Another option is to first select the expression, and then hold down the left mouse button to drag the selected code unto the ‘Watch’ window:

Visual Studio's Watch: track C# code (6)

The third approach is to double-click on an empty row in the ‘Watch’ window. Then type in an expression followed by Enter.

Visual Studio's Watch: track C# code (7)

The fourth option is when you have the ‘QuickWatch’ window open. There press the ‘Add Watch’ button to more the expression from ‘QuickWatch’ to the ‘Watch’ window.

Visual Studio's Watch: track C# code (8)

We typically add variables and expressions to the ‘Watch’ window. But we can add practically any valid C# code to that window – including methods and objects (Dorman, 2010).

# Track expressions in the ‘Watch’ window

When we have an expression in the ‘Watch’ window, we can see how it changes as we step through the code. That gives a good idea of how the program behaves and where its behaviour is different from what we expect.

Say we have the following expressions in our ‘Watch’ window:

Visual Studio's Watch: track C# code (9)

Now say we place a breakpoint and then start debugging. Then just before our program executes the first breakpoint it pauses under the debugger. That allows us to see what values the expressions in the ‘Watch’ window have at that exact point in the program:

Visual Studio's Watch: track C# code (10)

In this example royaltyAmount has a value of 0 and both Boolean expressions are false. Then when we step through the code with ‘Step Into’ (F11) we see how the expressions change:

Visual Studio's Watch: track C# code (11)

Because the line just executed initialised our booksWritten variable to 2, the booksWritten > 1 expression in the ‘Watch’ window changes from false to true. That value in ‘Watch’ also highlights in red to show it just updated.

Let’s continue with this example and execute the next line of code. That line sets the royaltyAmount variable to an initial value of 0.15, as the ‘Watch’ window also shows:

Visual Studio's Watch: track C# code (12)

When we arrive at the example’s if statement, the ‘Watch’ window helps to figure out why the nested if statement doesn’t execute. Let’s see how:

Visual Studio's Watch: track C# code (13)

We see here that the nested if statement evaluates two expressions that, thanks to the logical AND operator (&&), both need to be true. The first expression checks if booksWritten is greater than 1, which is true as we see in the ‘Watch’ window. But the second expression wants royaltyAmount to be bigger than 15, which is not the case according to ‘Watch’.

Actually, ‘Watch’ shows that royaltyAmount has a value of 0.15 instead 15. When we realise those royalties are a percentage, the bug is likely that we mixed up their numerical representation (that is, using an integer to express a percentage instead of a decimal value).

Even though this is a basic example, it already shows how ‘Watch’ helps to check assumptions our code makes.

# Change values in the ‘Watch’ window

Another feature of the ‘Watch’ window is that we can change the values of its variables. Then when we step through the code under the debugger, we can see how those changed values affect our program. This way we can easily experiment and test out ideas to help locate the bug.

To change a variable in the ‘Watch’ window, we double-click on the variable’s field in the ‘Value’ column. That makes a text selection appear to edit the variable:

Visual Studio's Watch: track C# code (14)

We then type in a new value (let’s say 20) and press Enter. That highlights the variable’s value in red to show it recently changed:

Visual Studio's Watch: track C# code (15)

When we adjust a variable in the ‘Watch’ window, other expressions may change too. For instance, when we changed royaltyAmount to 20, the expression royaltyAmount >= 15 went from false to true (and is now highlighted in red too).

Expressions that we place in the ‘Watch’ window remain there across multiple debugging sessions. But values we change only apply to the current session.

So when we stop and start debugging again, we’ll need to change the values in ‘Watch’ again.

Let’s see how changing values in ‘Watch’ affects stepping through the code under the debugger.

In the image below the debugger is about to execute the Console.WriteLine() line highlighted in yellow. That line is inside a nested if statement which checks if booksWritten is bigger than 1 and royaltyAmount 15 or more. Those expressions are both true – we’ve just changed royaltyAmount to 20, after all.

Because we adjusted royaltyAmount in the ‘Watch’ window, the Console.WriteLine() line can now execute and we can explore how this affects our program.

Visual Studio's Watch: track C# code (16)

A changed variable in the ‘Watch’ window keeps its new value as we step through the code. But when we execute a line of code that changes its value, the variable changes with it.

And so the ‘Watch’ window does not prevent our program from changing the variable.

# Remove values from the ‘Watch’ window

The expressions in the ‘Watch’ window remain there across debugging sessions. And so when we don’t need them the next time we start debugging, we better remove them.

To remove an expression from ‘Watch’ we right-click on it and then select ‘Delete Watch’. Or we select the expression and press the Delete key.

Visual Studio's Watch: track C# code (17)

To empty the entire ‘Watch’ window we right-click somewhere in this window and select ‘Clear All’. (Or we press Ctrl + A to select all watches and remove them with Delete.)

Visual Studio's Watch: track C# code (18)

# Summary

To troubleshoot C# code we often run our program under a debugger. That makes it possible to explore our program at different points while it runs. We start debugging in Visual Studio with F5 (‘Start Debugging’;Visual Studio's Watch: track C# code (19)), F11 (‘Step Into’;Visual Studio's Watch: track C# code (20)), or the ‘Run To Cursor’ option. Once our program pauses under the debugger we likely want to keep track of some variables and expressions we suspect cause the bug.

We do that with the ‘Watch’ window. That window, of which Visual Studio actually has multiple (‘Watch 1’ through ‘Watch 4’), shows the current values of expressions. When we step through our program and execute code line by line, the expressions in ‘Watch’ update and we see how the code affects those values. This way we can keep an eye on (or watch) the code elements we suspect cause the bug.

Besides tracking expressions we can also change them in the ‘Watch’ window. This makes it possible to quickly see how the program would behave with different values, without us needing to stop debugging and change the code.

References

Asad, A. & Ali, H. (2017). The C# Programmer’s Study Guide (MCSD). New York, NY: Apress.

Dorman, S. (2010). Sams Teach Yourself Visual C# 2010 in 24 Hours. Indianapolis, IN: Sams/Pearson Education.

Liberty, J. & MacDonald, B. (2009). Learning C# 3.0: Master the Fundamentals of C# 3.0. Sebastopol, CA: O’Reilly Media.

Stephens, R. (2014). C# 5.0 Programmer Reference. Indianapolis, IN: John Wiley & Sons.

Last updated on (published ).

  • Step through C# code: run a program line by line in Visual Studio

    In Visual Studio we can analyse our C# program line by line. This tutorial explores stepping through a C# application under the code debugger.

  • How to start and stop debugging C# code in Visual Studio?

    Before we can fix our C# programming code in Visual Studio, we need to start and stop the debugger. Learn more about that in this article.

  • Debug example: fix C# if statements in Visual Studio

    Knowing where to start is often half the debugging work. This tutorial looks at debugging C# if statements, including tips for quick troubleshooting.

  • Quickly debug expressions with Visual Studio’s ‘QuickWatch’

    When debugging C# code, it often helps to quickly see what some expression evaluates to. The ‘QuickWatch’ window helps us do that and more.

  • Quickly debug C# code with Visual Studio’s ‘Run To Cursor’ option

    Before we can analyse and debug our C# program in Visual Studio, we have to start debugging. A quick way to do that is with ‘Run To Cursor’.

« All C# visual studio articles

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated: 02/17/2023

Views: 6358

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.