Visual Studio 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. You can debug your variables in the Visual Studio integrated development environment.

Debug menu


First here we look at a short program written in the C# language that has several local variables in it. To look inside your locals, you need to set a breakpoint somewhere in the block of C# code you have.

Program that uses local variables: C#

using System;

class Program
    static void Main()
	// A.
	// An integer is local.
	int cat = 1;

	// B.
	// A string is local.
	string dog = "cute";

	// C.
	// An array is local.
	bool[] b = new bool[]

Debugging breakpoint

Here in the tutorial we will 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. 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. The red block over the code line is where the debugger will open.

Screenshot of breakpoint in Visual Studio

When the debugger will open. When you set the breakpoint red circle on your code, the debugger will open only if that point is hit. This makes it possible to use breakpoints to alert you if something unexpected happens.

Start debugging

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. The icons on your setup will look a little different depending on your preferences and OS.

The breakpoint 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. Please see the screenshot for visual reference.

Next:Open the Locals window. Like many windows in Visual Studio 2008, 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 integrated development environment related to local variable debugging. In Visual Studio 2008, you can see Locals' values. You can browse hierarchies of the locals' internals. You can visualize data in a few ways too.

Locals window in Visual Studio

Name:The name of your variable.

Value:The data stored in your variable.

Type:The data type of your variable, such as int, string, bool[].

Text Visualizer:Allows you to see the plain text value in the variable.

XML Visualizer:For XML files. Probably uses the XML engine from Internet Explorer.

HTML Visualizer:Use to view the data in Internet Explorer as HTML.

Expression:This is on the Visualizer windows. Shows the variable name.

Step Over

Let's look at step options in the Visual Studio debugger. 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.

Debug menu in Visual Studio: step

Step Into, F11:Use Step Into to go inside your method and stop immediately inside it.

Step Over:This also moves to the next step in your code. It doesn't, however, break inside any methods.

Step Out:Ignores the rest of the current method and goes to the calling method.

Step example in this tutorial. 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.


Array type

Here we examine the array type in the C# programming language 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.

Array elements

- b              - name of the array
    [0] true     - first value in array
    [1] false    - second value in array
    [2] true     - third
    [3] true     - fourth
Locals window showing bools

Note:The screenshot shows the local arrays. Each element in the array is referenced by an index. Look inside an array by clicking on the + box.

More information

The Visual Studio 2008 debugger is thorough and effective for most problems. It has many more options, including Call Stack, Command Window, Exceptions, and Autos. You can configure breakpoints to simply write messages to the console, or to keep counts of how many times they are hit. This is great for performance testing.

Activation Record: Call Stack

Disable debugger

Squares: abstract

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. The debugger causes significant performance overhead. In my experience it takes close to twice the time to execute Debug programs.

Method 1:In your ASP.NET website, open Web.config and change the compilation tag's debug attribute to false.

Method 2:In Windows Forms or Console 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.


Other methods are available to you for debugging complex or simple programs written in the C# programming language. If you are using a console program, the Console.WriteLine method (or Console.Write) are useful. This doesn't involve the Visual Studio debugging environment so can be used when testing release builds and for accurate performance testing.


Debug methods

Framework: NET

The .NET Framework provides a variety of useful methods in the Debug class that can be used to hard-code debugging facilities in your programs. Some methods that you can use are Debug.Write and Debug.Assert. The benefit to these methods over the Console methods is that they are removed in Release builds of your application.

Debug.Write Method


C# language

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.

Tip:In the C language, programmers use "printf" statements, which are like Console.Write in the C# language.

However:Using Locals in the debugger is easier for many developers. It makes code easier to maintain.

C#: .NET: VS