Tutorial: Debug a .NET console application using Visual Studio
This tutorial introduces the debugging tools available in Visual Studio.
Important
All of the keyboard shortcuts are based on the defaults from Visual Studio. Your keyboard shortcuts may vary, for more information see Keyboard shortcuts in Visual Studio.
Prerequisites
- This tutorial works with the console app that you create in Create a .NET console application using Visual Studio.
Use Debug build configuration
Debug and Release are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution.
In the Debug configuration, a program compiles with full symbolic debug information and no optimization. Optimization complicates debugging, because the relationship between source code and generated instructions is more complex. The release configuration of a program has no symbolic debug information and is fully optimized.
By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging.
Start Visual Studio.
Open the project that you created in Create a .NET console application using Visual Studio.
The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app:
Set a breakpoint
A breakpoint temporarily interrupts the execution of the application before the line with the breakpoint is executed.
Set a breakpoint on the line that displays the name, date, and time, by clicking in the left margin of the code window on that line. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by placing the cursor in the line of code and then pressing F9 or choosing Debug > Toggle Breakpoint from the menu bar.
As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin.
Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu.
Enter a string in the console window when the program prompts for a name, and then press Enter.
Program execution stops when it reaches the breakpoint and before the
Console.WriteLine
method executes. The Locals window displays the values of variables that are defined in the currently executing method.
Use the Immediate window
The Immediate window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program.
If the Immediate window is not visible, display it by choosing Debug > Windows > Immediate.
Enter
name = "Gracie"
in the Immediate window and press the Enter key.Enter
currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()
in the Immediate window and press the Enter key.The Immediate window displays the value of the string variable and the properties of the DateTime value. In addition, the values of the variables are updated in the Locals window.
Press F5 to continue program execution. Another way to continue is by choosing Debug > Continue from the menu.
The values displayed in the console window correspond to the changes you made in the Immediate window.
Press any key to exit the application and stop debugging.
Set a conditional breakpoint
The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a conditional breakpoint.
Right-click on the red dot that represents the breakpoint. In the context menu, select Conditions to open the Breakpoint Settings dialog. Select the box for Conditions if it's not already selected.
For the Conditional Expression, enter the following code in the field that shows example code that tests if
x
is 5.string.IsNullOrEmpty(name)
String.IsNullOrEmpty(name)
Each time the breakpoint is hit, the debugger calls the
String.IsNullOrEmpty(name)
method, and it breaks on this line only if the method call returnstrue
.Instead of a conditional expression, you can specify a hit count, which interrupts program execution before a statement is executed a specified number of times. Another option is to specify a filter condition, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name.
Select Close to close the dialog.
Start the program with debugging by pressing F5.
In the console window, press the Enter key when prompted to enter your name.
Because the condition you specified (
name
is eithernull
or String.Empty) has been satisfied, program execution stops when it reaches the breakpoint and before theConsole.WriteLine
method executes.Select the Locals window, which shows the values of variables that are local to the currently executing method. In this case,
Main
is the currently executing method. Observe that the value of thename
variable is""
, or String.Empty.Confirm the value is an empty string by entering the following statement in the Immediate window and pressing Enter. The result is
true
.? name == String.Empty
? String.IsNullOrEmpty(name)
The question mark directs the immediate window to evaluate an expression.
Press F5 to continue program execution.
Press any key to close the console window and stop debugging.
Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing Debug > Toggle Breakpoint while the line of code is selected.
Step through a program
Visual Studio also allows you to step line by line through a program and monitor its execution. Ordinarily, you'd set a breakpoint and follow program flow through a small part of your program code. Since this program is small, you can step through the entire program.
Choose Debug > Step Into. Another way to debug one statement at a time is by pressing F11.
Visual Studio highlights and displays an arrow beside the next line of execution.
C#
Visual Basic
At this point, the Locals window shows that the
args
array is empty, andname
andcurrentDate
have default values. In addition, Visual Studio has opened a blank console window.Press F11. Visual Studio now highlights the next line of execution. The Locals window is unchanged, and the console window remains blank.
C#
Visual Basic
Press F11. Visual Studio highlights the statement that includes the
name
variable assignment. The Locals window shows thatname
isnull
, and the console window displays the string "What is your name?".Respond to the prompt by entering a string in the console window and pressing Enter. The console is unresponsive, and the string you entered isn't displayed in the console window, but the Console.ReadLine method will nevertheless capture your input.
Press F11. Visual Studio highlights the statement that includes the
currentDate
variable assignment. The Locals window shows the value returned by the call to the Console.ReadLine method. The console window also displays the string you entered at the prompt.Press F11. The Locals window shows the value of the
currentDate
variable after the assignment from the DateTime.Now property. The console window is unchanged.Press F11. Visual Studio calls the Console.WriteLine(String, Object, Object) method. The console window displays the formatted string.
Choose Debug > Step Out. Another way to stop step-by-step execution is by pressing Shift+F11.
The console window displays a message and waits for you to press a key.
Press any key to close the console window and stop debugging.
Use Release build configuration
Once you've tested the Debug version of your application, you should also compile and test the Release version. The Release version incorporates compiler optimizations that can sometimes negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications.
To build and test the Release version of your console application, change the build configuration on the toolbar from Debug to Release.
When you press F5 or choose Build Solution from the Build menu, Visual Studio compiles the Release version of the application. You can test it as you did the Debug version.
Next steps
In this tutorial, you used Visual Studio debugging tools. In the next tutorial, you publish a deployable version of the app.
This tutorial introduces the debugging tools available in Visual Studio.
Important
All of the keyboard shortcuts are based on the defaults from Visual Studio. Your keyboard shortcuts may vary, for more information see Keyboard shortcuts in Visual Studio.
Prerequisites
- This tutorial works with the console app that you create in Create a .NET console application using Visual Studio.
Use Debug build configuration
Debug and Release are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution.
In the Debug configuration, a program compiles with full symbolic debug information and no optimization. Optimization complicates debugging, because the relationship between source code and generated instructions is more complex. The release configuration of a program has no symbolic debug information and is fully optimized.
By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging.
Start Visual Studio.
Open the project that you created in Create a .NET console application using Visual Studio.
The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app:
Set a breakpoint
A breakpoint temporarily interrupts the execution of the application before the line with the breakpoint is executed.
Set a breakpoint on the line that displays the name, date, and time, by clicking in the left margin of the code window on that line. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by placing the cursor in the line of code and then pressing F9 or choosing Debug > Toggle Breakpoint from the menu bar.
As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin.
Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu.
Enter a string in the console window when the program prompts for a name, and then press Enter.
Program execution stops when it reaches the breakpoint and before the
Console.WriteLine
method executes. The Locals window displays the values of variables that are defined in the currently executing method.
Use the Immediate window
The Immediate window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program.
If the Immediate window is not visible, display it by choosing Debug > Windows > Immediate.
Enter
name = "Gracie"
in the Immediate window and press the Enter key.Enter
currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()
in the Immediate window and press the Enter key.The Immediate window displays the value of the string variable and the properties of the DateTime value. In addition, the values of the variables are updated in the Locals window.
Press F5 to continue program execution. Another way to continue is by choosing Debug > Continue from the menu.
The values displayed in the console window correspond to the changes you made in the Immediate window.
Press any key to exit the application and stop debugging.
Set a conditional breakpoint
The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a conditional breakpoint.
Right-click on the red dot that represents the breakpoint. In the context menu, select Conditions to open the Breakpoint Settings dialog. Select the box for Conditions if it's not already selected.
For the Conditional Expression, enter the following code in the field that shows example code that tests if
x
is 5.string.IsNullOrEmpty(name)
String.IsNullOrEmpty(name)
Each time the breakpoint is hit, the debugger calls the
String.IsNullOrEmpty(name)
method, and it breaks on this line only if the method call returnstrue
.Instead of a conditional expression, you can specify a hit count, which interrupts program execution before a statement is executed a specified number of times. Another option is to specify a filter condition, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name.
Select Close to close the dialog.
Start the program with debugging by pressing F5.
In the console window, press the Enter key when prompted to enter your name.
Because the condition you specified (
name
is eithernull
or String.Empty) has been satisfied, program execution stops when it reaches the breakpoint and before theConsole.WriteLine
method executes.Select the Locals window, which shows the values of variables that are local to the currently executing method. In this case,
Main
is the currently executing method. Observe that the value of thename
variable is""
, or String.Empty.Confirm the value is an empty string by entering the following statement in the Immediate window and pressing Enter. The result is
true
.? name == String.Empty
? String.IsNullOrEmpty(name)
The question mark directs the immediate window to evaluate an expression.
Press F5 to continue program execution.
Press any key to close the console window and stop debugging.
Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing Debug > Toggle Breakpoint while the line of code is selected.
Step through a program
Visual Studio also allows you to step line by line through a program and monitor its execution. Ordinarily, you'd set a breakpoint and follow program flow through a small part of your program code. Since this program is small, you can step through the entire program.
Choose Debug > Step Into. Another way to debug one statement at a time is by pressing F11.
Visual Studio highlights and displays an arrow beside the next line of execution.
C#
Visual Basic
At this point, the Locals window shows that the
args
array is empty, andname
andcurrentDate
have default values. In addition, Visual Studio has opened a blank console window.Press F11. Visual Studio now highlights the next line of execution. The Locals window is unchanged, and the console window remains blank.
C#
Visual Basic
Press F11. Visual Studio highlights the statement that includes the
name
variable assignment. The Locals window shows thatname
isnull
, and the console window displays the string "What is your name?".Respond to the prompt by entering a string in the console window and pressing Enter. The console is unresponsive, and the string you entered isn't displayed in the console window, but the Console.ReadLine method will nevertheless capture your input.
Press F11. Visual Studio highlights the statement that includes the
currentDate
variable assignment. The Locals window shows the value returned by the call to the Console.ReadLine method. The console window also displays the string you entered at the prompt.Press F11. The Locals window shows the value of the
currentDate
variable after the assignment from the DateTime.Now property. The console window is unchanged.Press F11. Visual Studio calls the Console.WriteLine(String, Object, Object) method. The console window displays the formatted string.
Choose Debug > Step Out. Another way to stop step-by-step execution is by pressing Shift+F11.
The console window displays a message and waits for you to press a key.
Press any key to close the console window and stop debugging.
Use Release build configuration
Once you've tested the Debug version of your application, you should also compile and test the Release version. The Release version incorporates compiler optimizations that can sometimes negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications.
To build and test the Release version of your console application, change the build configuration on the toolbar from Debug to Release.
When you press F5 or choose Build Solution from the Build menu, Visual Studio compiles the Release version of the application. You can test it as you did the Debug version.
Next steps
In this tutorial, you used Visual Studio debugging tools. In the next tutorial, you publish a deployable version of the app.
This tutorial introduces the debugging tools available in Visual Studio.
Prerequisites
- This tutorial works with the console app that you create in Create a .NET console application using Visual Studio.
Use Debug build configuration
Debug and Release are Visual Studio's built-in build configurations. You use the Debug build configuration for debugging and the Release configuration for the final release distribution.
In the Debug configuration, a program compiles with full symbolic debug information and no optimization. Optimization complicates debugging, because the relationship between source code and generated instructions is more complex. The release configuration of a program has no symbolic debug information and is fully optimized.
By default, Visual Studio uses the Debug build configuration, so you don't need to change it before debugging.
Start Visual Studio.
Open the project that you created in Create a .NET console application using Visual Studio.
The current build configuration is shown on the toolbar. The following toolbar image shows that Visual Studio is configured to compile the Debug version of the app:
Set a breakpoint
A breakpoint temporarily interrupts the execution of the application before the line with the breakpoint is executed.
Set a breakpoint on the line that displays the name, date, and time, by clicking in the left margin of the code window on that line. The left margin is to the left of the line numbers. Other ways to set a breakpoint are by placing the cursor in the line of code and then pressing F9 or choosing Debug > Toggle Breakpoint from the menu bar.
As the following image shows, Visual Studio indicates the line on which the breakpoint is set by highlighting it and displaying a red dot in the left margin.
Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu.
Enter a string in the console window when the program prompts for a name, and then press Enter.
Program execution stops when it reaches the breakpoint and before the
Console.WriteLine
method executes. The Locals window displays the values of variables that are defined in the currently executing method.
Use the Immediate window
The Immediate window lets you interact with the application you're debugging. You can interactively change the value of variables to see how it affects your program.
If the Immediate window is not visible, display it by choosing Debug > Windows > Immediate.
Enter
name = "Gracie"
in the Immediate window and press the Enter key.Enter
currentDate = DateTime.Parse("2019-11-16T17:25:00Z").ToUniversalTime()
in the Immediate window and press the Enter key.The Immediate window displays the value of the string variable and the properties of the DateTime value. In addition, the values of the variables are updated in the Locals window.
Press F5 to continue program execution. Another way to continue is by choosing Debug > Continue from the menu.
The values displayed in the console window correspond to the changes you made in the Immediate window.
Press any key to exit the application and stop debugging.
Set a conditional breakpoint
The program displays the string that the user enters. What happens if the user doesn't enter anything? You can test this with a useful debugging feature called a conditional breakpoint.
Right-click on the red dot that represents the breakpoint. In the context menu, select Conditions to open the Breakpoint Settings dialog. Select the box for Conditions if it's not already selected.
For the Conditional Expression, enter the following code in the field that shows example code that tests if
x
is 5.String.IsNullOrEmpty(name)
String.IsNullOrEmpty(name)
Each time the breakpoint is hit, the debugger calls the
String.IsNullOrEmpty(name)
method, and it breaks on this line only if the method call returnstrue
.Instead of a conditional expression, you can specify a hit count, which interrupts program execution before a statement is executed a specified number of times. Another option is to specify a filter condition, which interrupts program execution based on such attributes as a thread identifier, process name, or thread name.
Select Close to close the dialog.
Start the program with debugging by pressing F5.
In the console window, press the Enter key when prompted to enter your name.
Because the condition you specified (
name
is eithernull
or String.Empty) has been satisfied, program execution stops when it reaches the breakpoint and before theConsole.WriteLine
method executes.Select the Locals window, which shows the values of variables that are local to the currently executing method. In this case,
Main
is the currently executing method. Observe that the value of thename
variable is""
, or String.Empty.Confirm the value is an empty string by entering the following statement in the Immediate window and pressing Enter. The result is
true
.? name == String.Empty
? String.IsNullOrEmpty(name)
The question mark directs the immediate window to evaluate an expression.
Press F5 to continue program execution.
Press any key to close the console window and stop debugging.
Clear the breakpoint by clicking on the dot in the left margin of the code window. Other ways to clear a breakpoint are by pressing F9 or choosing Debug > Toggle Breakpoint while the line of code is selected.
Step through a program
Visual Studio also allows you to step line by line through a program and monitor its execution. Ordinarily, you'd set a breakpoint and follow program flow through a small part of your program code. Since this program is small, you can step through the entire program. While not currently debugging, and focus is on Visual Studio:
Choose Debug > Step Into. Another way to debug one statement at a time is by pressing F11.
Visual Studio highlights and displays an arrow beside the next line of execution.
C#
Visual Basic
At this point, the Locals window shows that the
args
array is empty, andname
andcurrentDate
have default values. In addition, Visual Studio has opened a blank console window.Press F11. Visual Studio now highlights the next line of execution. The Locals window is unchanged, and the console window remains blank.
C#
Visual Basic
Press F11. Visual Studio highlights the statement that includes the
name
variable assignment. The Locals window shows thatname
isnull
, and the console window displays the string "What is your name?".Respond to the prompt by entering a string in the console window and pressing Enter. The console is unresponsive, and the string you entered isn't displayed in the console window, but the Console.ReadLine method will nevertheless capture your input.
Press F11. Visual Studio highlights the statement that includes the
currentDate
variable assignment. The Locals window shows the value returned by the call to the Console.ReadLine method. The console window also displays the string you entered at the prompt.Press F11. The Locals window shows the value of the
currentDate
variable after the assignment from the DateTime.Now property. The console window is unchanged.Press F11. Visual Studio calls the Console.WriteLine(String, Object, Object) method. The console window displays the formatted string.
Choose Debug > Step Out. Another way to stop step-by-step execution is by pressing Shift+F11.
The console window displays a message and waits for you to press a key.
Press any key to close the console window and stop debugging.
Use Release build configuration
Once you've tested the Debug version of your application, you should also compile and test the Release version. The Release version incorporates compiler optimizations that can sometimes negatively affect the behavior of an application. For example, compiler optimizations that are designed to improve performance can create race conditions in multithreaded applications.
To build and test the Release version of your console application, change the build configuration on the toolbar from Debug to Release.
When you press F5 or choose Build Solution from the Build menu, Visual Studio compiles the Release version of the application. You can test it as you did the Debug version.
Next steps
In this tutorial, you used Visual Studio debugging tools. In the next tutorial, you publish a deployable version of the app.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for