C# Visual Studio Debugging TutorialUse Visual Studio to debug programs. Learn to examine the contents of variables.
Debugging. The values of local variables change as a program executes. It is possible to see these changes. This is useful for when you want to make sure the values are correct.
Visual Studio. You can debug your variables in the Visual Studio integrated development environment. This is a short tutorial. In it, we deal with local variables.
First here we look at a short program that has several local variables in it. To look inside your locals, you need to set a breakpoint somewhere in the code.
First: Please start up your version of Visual Studio and create a new C# console application.
C# program that uses local variables
static void Main()
// An integer is local.
int cat = 1;
// A string is local.
string dog = "cute";
// An array is local.
bool b = new bool
Here we add a breakpoint near the end of the above program. On the far left of your source code tab, you will see a narrow gray strip.
Then: Click on it in the line you want to debug. The red circle on the left is where you need to click to set the debugging breakpoint.
And: The red block over the code line is where the debugger will open. We must first execute the program.
Point hit. When you set the breakpoint red circle on your code, the debugger will open only if that point is hit. So breakpoints can alert you if something unexpected happens.
You can initialize the debugger at the same time as starting your program in Visual Studio. To do this, click on the green arrow in your Visual Studio toolbar.
And: The icons on your setup will look a little different depending on your preferences and OS.
will be hit as the program executes. The red blocks will turn yellow and you may see different panes such as the Call Stack pane.
Next: Open the Locals window. You can open the Locals pane in more than one way. Go to Debug -> Windows -> Locals.
Here we look at some features in the Visual Studio IDE related to local variable debugging. You can browse hierarchies of the locals' internals.
Note: Many of these options are advanced and not needed for most debugging purposes.
Properties in debugger: Visual Studio
The name of your variable.
The data stored in your variable.
The data type of your variable, such as int, string, bool.
Allows you to see the plain text value in the variable.
For XML files. You can visualize XML data with this function.
Use to view the data in Internet Explorer as HTML.
This is on the Visualizer windows. Shows the variable name.
Let's look at step options. Often when debugging, you want to advance one step. Clicking the green arrow in the toolbar to Continue will take you to the next breakpoint.
Note: Using these options takes practice. It is hard to learn them at the same time you are learning an entire language.
Step options: Visual Studio
Step Into, F11:
Use Step Into to go inside your method and stop immediately inside it.
This also moves to the next step in your code.
It doesn't, however, break inside any methods.
Ignores the rest of the current method and goes to the calling method.
Step example. In our example, we want to use Step Over. This will enable us to see the values of all of the variables. The final variable is assigned at the end of the method.
Here we examine the array type as it appears during execution in the Visual Studio debugger. This is an effective way to monitor the values of elements in an array during runtime.
Note: The screenshot shows the local arrays. Each element in the array is referenced by an index.
Tip: Look inside an array by clicking on the + box. You can browse all elements.
- b - name of the array
 true - first value in array
 false - second value in array
 true - third
 true - fourth
The debugger has many more options, including Call Stack, Command Window, Exceptions, and Autos. You can configure breakpoints.
And: With break points, we can write messages to the console, or keep counts of how many times they are hit.
Before you ever deploy your web site or Windows Forms application, disable debugging. This will eliminate the features in this article but you can re-enable it later.
Method 1: In your ASP.NET website, open Web.config and change the compilation tag's debug attribute to false.
Method 2: In Windows applications, go to Project -> MyApplication Properties. Then change the Configure dropdown to Release.
Method 3: The third way is to change the toolbar dropdown when it is available.
The debugger causes significant performance overhead. In my experience it takes close to twice the time to execute Debug programs.
And: This depends on the program. More complex, algorithmic programs tend to become still slower.
Benchmark: For benchmarking, please run outside of the debugger. For the best benchmarks, do not involve Visual Studio at all.Benchmark
We can use the "Edit Value" feature to change the value of a variable in a program as it runs. Try this. On the second if-statement, add a breakpoint.
Then: When the breakpoint is hit, right click on its value in the Locals window. And change its value to 3.
Result: The value "true" will be printed twice. We edited the value to be 2 and then 3.
Program for Edit Value demonstration: C#
static void Main()
int value = 2;
if (value == 2)
if (value == 3)
Other methods are available to you for debugging complex or simple programs. If you are using a console program, the Console.WriteLine method (or Console.Write) are useful.
Note: This doesn't involve the VS debugging environment. So they can be used when testing release builds and for performance testing.Console
The .NET Framework provides methods in the Debug class that can be used to hard-code debugging facilities. Some methods that you can use are Debug.Write and Debug.Assert.
Tip: The benefit to these methods over the Console methods is that they are removed in Release builds of your application.Debug.Write
A summary. We looked inside the variables in methods using the Visual Studio debugger. Local variables are part of every program, and writing them out with Console.Write is cumbersome.
With the debugger, we eliminate lots of manual work. In the C language, programmers use "printf" statements, which are like Console.Write. But using Locals in the debugger is easier.
As time passes, I update this tutorial. I created it originally for Visual Studio 2008. But this current version targets Visual Studio 2013.
© 2007-2020 Sam Allen. Send bug reports to email@example.com.