Know Your Debugger Part 2: Ok, I'm Broken. Now What?
Join the DZone community and get the full member experience.
Join For FreeSo now you know how to set breakpoints and pause execution of your application. There are a lot of really cool things you can do in this state. You can inspect variable values and even change them (more on that part later). If you continue to step through your code you can follow execution line by line. This can be very helpful if your application is not behaving as you expect or if you need to just confirm that a certain if statement is evaluated properly. My biggest use for the debugger has been to verify execution and hunt down issues.
I'm sure that all of us at some point have needed to find out the value of an object or variable in code and put maybe a Console.WriteLine or maybe a javascript alert to give you the value. While this technically works, the debugger can help you so much more. Below is a method that I will debug to demonstrate the awesomeness of the Visual Studio debugger.

You can see that the value is 3 because that's what we passed into the method. Note that the breakpoint defines the line that is about to get executed so the addition of 2 to the value hasn't happened yet. If I hit F10 to step over and mouse over value again, you can see it is now 5:

The debugger is now on the line that will take the value and multiply it by 5. If I hit F10 again, you can see that the value is now 25 as expected:

If you continue to step through this you will see how the value changes and how that the string description is getting set properly. While mousing over variables certainly works, you may want to use the watch windows if there is a variable or statement that you want to monitor over time. When your code is broken, you can right click on value and select either "Add Watch" or "Add Quick Watch". "Add Watch" will add the variable to the watch window for the life of the Visual Studio session and "Add Quick Watch" will pop up a smaller watch window that will go away once you are done with the debugging session:
Add Watch Window:

Quick Watch Popup:

You don't have to click on the variables and add them to get them in a watch window, you also can just type them in a new line. There is even intellisense when doing so. What is really cool about the watch windows is that you can watch any variable or even statements. I can go ahead and add a watch of (value == 25) and you can see that if the value is not 25 it will show false:

And as soon as the value is 25, it will show true:

The debugger is very powerful and can give you some additional tools to make sure that your application is behaving as expected or can help you track down issues. One thing to note however, if you are inspecting an IQueryable<T> with the debugger, the debugger will cause it to be queried and also any IEnumerable will be enumerated. This can lead to some unexpected results as you may not be ready to query or enumerate over the collection. If you haven't worked with watch windows before, Give it a try, you won't be disappointed.
I'm sure that all of us at some point have needed to find out the value of an object or variable in code and put maybe a Console.WriteLine or maybe a javascript alert to give you the value. While this technically works, the debugger can help you so much more. Below is a method that I will debug to demonstrate the awesomeness of the Visual Studio debugger.
int CrazyMath(int value)There a few things going on here, mainly the changing of the "value" int that is passed in to the CrazyMath method. If we set a breakpoint on line {line number} we can inspect the value at this point by mousing over the variable:
{
value += 2;
value *= 5;
string description = string.Format("The value is: {1}", value);
value = 7;
return value;
}

You can see that the value is 3 because that's what we passed into the method. Note that the breakpoint defines the line that is about to get executed so the addition of 2 to the value hasn't happened yet. If I hit F10 to step over and mouse over value again, you can see it is now 5:

The debugger is now on the line that will take the value and multiply it by 5. If I hit F10 again, you can see that the value is now 25 as expected:

If you continue to step through this you will see how the value changes and how that the string description is getting set properly. While mousing over variables certainly works, you may want to use the watch windows if there is a variable or statement that you want to monitor over time. When your code is broken, you can right click on value and select either "Add Watch" or "Add Quick Watch". "Add Watch" will add the variable to the watch window for the life of the Visual Studio session and "Add Quick Watch" will pop up a smaller watch window that will go away once you are done with the debugging session:
Add Watch Window:

Quick Watch Popup:

You don't have to click on the variables and add them to get them in a watch window, you also can just type them in a new line. There is even intellisense when doing so. What is really cool about the watch windows is that you can watch any variable or even statements. I can go ahead and add a watch of (value == 25) and you can see that if the value is not 25 it will show false:

And as soon as the value is 25, it will show true:

The debugger is very powerful and can give you some additional tools to make sure that your application is behaving as expected or can help you track down issues. One thing to note however, if you are inspecting an IQueryable<T> with the debugger, the debugger will cause it to be queried and also any IEnumerable will be enumerated. This can lead to some unexpected results as you may not be ready to query or enumerate over the collection. If you haven't worked with watch windows before, Give it a try, you won't be disappointed.
application
Execution (computing)
Session (web analytics)
Data Types
Debug (command)
Strings
Monitor (synchronization)
JavaScript
Opinions expressed by DZone contributors are their own.
Comments