- article
Applies to: visual study
Visual Studio for Mac
visual study code
This article provides a step-by-step introduction to the features of the Visual Studio debugger. For a more advanced look at debugging features, seeFirst look at the debugger. when youdebugging your application, which usually means you're running the program with a debugger attached. By doing this, the debugger provides more ways to see what your code is doing at runtime. You can step through code and see values stored in variables, you can set watches on variables to see when values change, you can inspect the execution path of your code, see if a branch of code is executing, and more . If this is your first attempt at debugging your code, you may want to continueDebugging for absolute beginnersbefore reading this article.
While the demo application is Visual Basic, most features work in C#, C++, F#, Python, JavaScript, and other languages supported by Visual Studio (Edit and Continue are not supported for F#. F# and JavaScript are not supportedit waswindow). Screenshots are written in Visual Basic.
In this seminar you will:
- Start the debugger and hit a breakpoint.
- Learn about commands for passing code in the debugger
- Examine variables in datatips and debug windows
- Check the call stack
prerequisites
You must have Visual Studio 2019 installed and.NET Core cross-platform developmentworkload.
If you don't have Visual Studio installed, go toVisual Studio Downloadspage to install it for free.
If you need to install a workload but already have Visual Studio, go totool>Get tools and features…, which opens the Visual Studio installer. The Visual Studio installer will start. choose.NET Core cross-platform developmentworkload and selectAdjustment.
If you don't have Visual Studio installed, go toVisual Studio Downloadspage to install it for free.
If you need to install a workload but already have Visual Studio, go totool>Get tools and features…, which opens the Visual Studio installer. The Visual Studio installer will start. choose.NET Computing Developmentworkload and selectAdjustment.
create a project
First, create a .NET Core console application project. The project type comes with all the template files you need before adding anything!
Open image studio.
If the start window does not open, selectdocument>launch window.
Select in the launch windowcreate a new project.
highercreate a new projectwindow, enter or writecomfortin the search field. Then chooseVisual Basicsfrom the list of languages, then selectWindowsfrom the platform list.
After applying the language and platform filters, selectconsole applicationTemplates for .NET Core and selectNext.
noting
if you don't seeconsole applicationtemplate from which you can downloadcreate a new projectwindow. insideDidn't find what you were looking for?message, selectInstall more tools and featuresconnected. Then select in the Visual Studio installer.NET Core cross-platform developmentworkload.
indeConfigure your new projectwindow, type or enterstart troubleshootingindeThe name of the thingBox. Then chooseNext.
Select the recommended target framework (.NET Core 3.1) or .NET 5, and then selectcabinet.
Visual Studio opens your new project.
Open image studio.
If the start window does not open, selectdocument>launch window.
Select in the launch windowcreate a new project.
highercreate a new projectwindow, enter or writecomfortin the search field. Then chooseVisual Basicsfrom the list of languages, then selectWindowsfrom the platform list.
After applying the language and platform filters, selectconsole applicationTemplates for .NET Core and selectNext.
noting
if you don't seeconsole applicationtemplate from which you can downloadcreate a new projectwindow. insideDidn't find what you were looking for?message, selectInstall more tools and featuresconnected. Then select in the Visual Studio installer.NET Computing Developmentworkload.
indeConfigure your new projectwindow, type or enterstart troubleshootingindeThe name of the thingBox. Then chooseNext.
higherMore informationwindow, make sure the suggested target box is (.NET 6.0) and selectcabinet.
Visual Studio opens your new project.
application creation
existsprogram.vband replace all the default code with the following code:
εισαγωγή SystemClass ArrayExample Public Shared Sub Main() Dim Alphabet As Char() = {"f"c, "r"c, "e"c, "d"c, " "c, "s"c, "m"c , "i"c, "t"c, "h"c} Dim navn As String = "" Dim a As Heltal() = Nyt heltal(9) {} For i Som heltal = 0 Til bogstaver. Længde - 1 navn += bogstav(i) a(i) = i + 1 SendMessage(name, a(i)) Next Console.ReadKey() End Sub Private Shared Sub SendMessage(ByVal name As String, ByValmsg As Integer) Console.WriteLine ("Hello," & name & "!count to" & msg) τέλος δευτερεύουσας κατηγορίας
Start debugging!
accordingF5(Debug > Start Debugging) thestart troubleshootingbutton
in the debug toolbar.
F5Start the app with a debugger attached to the app process, but right now we haven't done anything special to test the code. So the app just loads and you see the console output.
hell! Count to 1 Hello, Fr! Count to 2 Hey Frey! Count to 3 Hello, Fred! Count to 4 Hello, Fred! Count to 5 Hello, Fred! Count to 6 Hi Fred SM! Count to 7 Hello, Fred Smith! Count to 8 Hello, Fred Smit! Count to 9 Hello, Fred Smith! count to 10
In this tutorial, we'll use the debugger to take a closer look at this application and learn about its debugging capabilities.
Press the red stop button to stop the debugger
button (TRANSPORT+F5).
In a console window, press any key to close the console window.
accordingF5(Debug > Start Debugging) or select the green onestart troubleshootingbutton on the debug toolbar.
F5Start the app with a debugger attached to the app process, but right now we haven't done anything special to test the code. So the app just loads and you see the console output.
hell! Count to 1 Hello, Fr! Count to 2 Hey Frey! Count to 3 Hello, Fred! Count to 4 Hello, Fred! Count to 5 Hello, Fred! Count to 6 Hi Fred SM! Count to 7 Hello, Fred Smith! Count to 8 Hello, Fred Smit! Count to 9 Hello, Fred Smith! count to 10
In this tutorial, we'll use the debugger to take a closer look at this application and learn about its debugging capabilities.
By pressing (TRANSPORT+F5) or select the red onestop debuggingbutton on the debug toolbar.
In a console window, press any key to close the console window.
Set a breakpoint and start the debugger
inde
to
cyclethe most important
function, set a breakpoint by clicking the left margin in the following line of code:name += letter(i)
a red circle
appears where you set the breakpoint.
Breakpoints are one of the most basic and important features for reliable debugging. Breakpoints indicate where Visual Studio should pause running code so you can see the value of variables, memory behavior, or if a code branch is in progress.
accordingF5thestart troubleshootingbutton
, the application starts and the debugger runs at the line of code where you set the breakpoint.
A yellow arrow indicates a statement where the debugger stopped, which also stopped the application execution at the same time (the statement has not yet been executed).
If the application is not already running,F5Start the debugger and stop at the first breakpoint. Or else,F5Continue program execution at the next breakpoint.
Breakpoints are a useful feature when you know which line of code or section of code you want to examine in detail. For information about the different types of breakpoints you can set, such as conditional breakpoints, seeuse breakpoints.
inde
to
cyclethe most important
function, set a breakpoint by clicking the left margin in the following line of code:name += letter(i)
A red circle appears where the breakpoint is set.
Breakpoints are one of the most basic and important features for reliable debugging. Breakpoints indicate where Visual Studio should pause running code so you can see the value of variables, memory behavior, or if a code branch is in progress.
accordingF5(Debug > Start Debugging) thestart troubleshootingbutton, the application starts and the debugger runs at the line of code where you set the breakpoint.
A yellow arrow indicates a statement where the debugger stopped, which also stopped the application execution at the same time (the statement has not yet been executed).
If the application is not already running,F5Start the debugger and stop at the first breakpoint. Or else,F5Continue program execution at the next breakpoint.
Breakpoints are a useful feature when you know which line of code or section of code you want to examine in detail. For information about the different types of breakpoints you can set, such as conditional breakpoints, seeuse breakpoints.
Use step commands to navigate through code in the debugger
Most of the time we use keyboard shortcuts here, as this is a great way to quickly run the application in the debugger (corresponding menu commands are shown in parentheses).
Although it was suspended on
to
in the loopthe most important
method, pressureF11(or choosedebug > enter) advances two more timesto send a message
method call.after pressureF11Twice you should have this line of code:
sendMessage(name, a(i))
accordingF11get back in
to send a message
method.The yellow pointer goes ahead to
to send a message
method.F11 isenterscommand and have the application execute one statement at a time. F11 is a great way to inspect the execution flow in as much detail as possible. (We'll also show you some other options for faster code browsing.) By default, the debugger skips non-user code (if you need more details, seejust my code).
Assuming you've checked
to send a message
method and want to exit the method but stay in the debugger. you can use itGo outsideSeries.accordingTRANSPORT+F11(theDebug > Split).
This command continues execution of the application (and advances the debugger) until the current method or function returns.
you should return
to
in the loopthe most important
method, break into send a message
method call.accordingF11several times until you return
to send a message
Call the method again.Press while a method call is pausedF10(or chooseTroubleshoot > Step In) once.
Note that this time the debugger will not pass
to send a message
method.F10Push the debugger without stepping through functions or methods in your application code (the code still runs). pressingF10existsto send a message
method call (instead ofF11), we skip the code implementationto send a message
(we may not be interested in this right now). For more information on the different methods of moving tags, seeCode navigation in the debugger.
In this article, we use keyboard shortcuts because this is a good way to quickly run the application in the debugger (corresponding commands such as menu commands are shown in parentheses).
Although it was suspended on
to
in the loopthe most important
method, pressureF11(or choosedebug > enter) advances two more timesto send a message
method call.after pressureF11Twice you should have this line of code:
sendMessage(name, a(i))
accordingF11get back in
to send a message
method.The yellow pointer goes ahead to
to send a message
method.F11Isenterscommand and have the application execute one statement at a time.F11is a great way to inspect the execution flow in as much detail as possible. (We'll also show you some other options for faster code browsing.) By default, the debugger skips non-user code (if you need more details, seejust my code).
Assuming you've checked
to send a message
method and want to exit the method but stay in the debugger. you can use itGo outsideSeries.accordingTRANSPORT+F11(theDebug > Split).
This command continues execution of the application (and advances the debugger) until the current method or function returns.
you should return
to
in the loopthe most important
method, break into send a message
method call.accordingF11several times until you return
to send a message
Call the method again.Press while a method call is pausedF10(or chooseTroubleshoot > Step In) once.
Note that this time the debugger will not pass
to send a message
method.F10Push the debugger without stepping through functions or methods in your application code (the code still runs). pressingF10existsto send a message
method call (instead ofF11), we skip the code implementationto send a message
(we may not be interested in this right now). For more information on the different methods of moving tags, seeCode navigation in the debugger.
Use the "Run to Click" navigation code.
accordingF5Proceed to the turning point again.
In the code editor, scroll down and hover
console.WriteLine
method ito send a message
method until greenrun to clickbuttonappears on the left. The button's tooltip says "Run to here".
noting
Thisrun to clickButtons are new in Visual Studio 2017. (If you don't see the green arrow button, useF11In this example, the debugger is moved to the correct location. )
clickrun to clickbutton
.
The debugger proceeds to
console.WriteLine
method.Using this button is equivalent to setting a temporary breakpoint.run to clickIt is convenient to quickly browse the visible area of \u200b\u200bthe application code (you can click on any open file).
accordingF5Proceed to the turning point again.
In the code editor, scroll down and hover
console.WriteLine
method ito send a message
method until greenrun to clickThe buttons appear on the left. The button's tooltip says "Run to here".chooserun to clickbutton.
The debugger proceeds to
console.WriteLine
method.Using this button is equivalent to setting a temporary breakpoint.run to clickIt is convenient to quickly browse the visible area of \u200b\u200bthe application code (you can click on any open file).
Quickly restart your app
clickrestart button on the debug toolbar (control key+TRANSPORT+F5).
when you pressrestart, which saves time compared to stopping the application and restarting the debugger. The debugger stops at the first breakpoint encountered during code execution.
The debugger stops again at the breakpoint you set earlier in the programto
ring.
To restart your app, tapcontrol key+TRANSPORT+F5key combination, which saves time compared to stopping the application and restarting the debugger. The debugger stops at the first breakpoint encountered during code execution.
The debugger stops again at the breakpoint you set earlier in the programto
ring.
Inspect variables using data hints
The ability to inspect variables is one of the most useful features of a debugger, and there are several ways to do it. Often when you're trying to debug a problem, you're trying to figure out if variables are storing the value you expect them to have at a particular time.
at break time
name += letter[i]
mouse pointer overletter
variable, you will see its default value, the value of the first element of the array,"f" c
.Then hold the mouse cursor over
Name
variable, you will see its current value, an empty string.accordingF5(theerror correction>To continue) enough times to repeat several times
to
loop, stop again at the breakpoint and hover overName
Each time the value of the variable is checked.The value of the variable changes with each iteration
to
loop , show valueeat
, AfterwardFR
, AfterwardFrey
, etc.When you fail, you often need a quick way to inspect property variable values to see if they store the value you expect, and data hints are a great way to do that.
The ability to inspect variables is one of the most useful features of a debugger, and there are several ways to do it. Often when you're trying to debug a problem, you're trying to figure out if variables are storing the value you expect them to have at a particular time.
at break time
name += letter[i]
mouse pointer overletter
variable, you will see its default value, the value of the first element of the array,"f" c
.Then hold the mouse cursor over
Name
variable, you will see its current value, an empty string.accordingF5(theerror correction>To continue) enough times to repeat several times
to
loop, stop again at the breakpoint and hover overName
Each time the value of the variable is checked.The value of the variable changes with each iteration
to
loop , show valueeat
, AfterwardFR
, AfterwardFrey
, etc.When you fail, you often need a quick way to inspect property variable values to see if they store the value you expect, and data hints are a great way to do that.
Examine the variables using the Autos and Locals windows
look at (thisit wasThe window at the bottom of the code editor.
If disabled, enable it when debugging is paused by selectingerror correction>Windows>it was.
indeit waswindow, you can see the variables and their current values. Thisit wasThe window displays all variables used by the current or previous line (see the documentation for language-specific behavior).
Then watchthe localwindow, in the tab next to itit waswindow.
expand
letter
variable to display the elements it contains.Thisthe localThe window shows the current variablefield of application, the current execution context.
look at (thisit wasThe window at the bottom of the code editor.
If disabled, enable it when debugging is paused by selectingerror correction>Windows>it was.
indeit waswindow, you can see the variables and their current values. Thisit wasThe window displays all variables used by the current or previous line (see the documentation for language-specific behavior).
Then watchthe localwindow, in the tab next to itit waswindow.
expand
letter
variable to display the elements it contains.Thisthe localThe window shows the current variablefield of application, the current execution context.
clock set
Right-click in the main code editor window
Name
variable and selectAdd to view.ThisI'm watchingA window opens at the bottom of the code editor. you can use itI'm watchingwindow to specify the variable (or expression) you want to focus on.
You have now set your watch to
Name
variable and you can see its value change as you move through the debugger. Unlike other resizable windows,I'm watchingThe window always shows the variables you are observing (they are grayed out when they are out of scope).
Check the call stack
Although it was suspended on
to
circle, clickcall stackwindow that opens by default in the lower right window.If disabled, enable it when debugging is paused by selectingerror correction>Windows>call stack.
clickF11several times until you see the debugger
to send a message
method. look at (thiscall stackwindow.Thiscall stackThe window shows the order in which the methods and functions are called. The top line shows the current function (
to send a message
method in this application). The second line showsto send a message
known as fromthe most important
method etc.noting
Thiscall stackThis window is similar to the debug perspective in some IDEs, such as Eclipse.
The call stack is a great way to inspect and understand the execution flow of an application.
You can double-click a line of code to view that source code, which also changes the current scope that the debugger is inspecting. This action does not advance the debugger.
You can also use the right-click menucall stackThe window does other things. For example, you can insert a breakpoint in a specified function by advancing the debugger withrun to the markerand then go to check the source code. For more information, seeHow to: Examine the call stack.
Although it was suspended on
to
circle, clickcall stackwindow that opens by default in the lower right window.If disabled, enable it when debugging is paused by selectingerror correction>Windows>call stack.
clickF11several times until you see the debugger
to send a message
method. look at (thiscall stackwindow.Thiscall stackThe window shows the order in which the methods and functions are called. The top line shows the current function (
to send a message
method in this application). The second line showsto send a message
known as fromthe most important
method etc.noting
Thiscall stackThis window is similar to the debug perspective in some IDEs, such as Eclipse.
The call stack is a great way to inspect and understand the execution flow of an application.
You can double-click a line of code to view that source code, which also changes the current scope that the debugger is inspecting. This action does not advance the debugger.
You can also use the right-click menucall stackThe window does other things. For example, you can insert a breakpoint in a specified function by advancing the debugger withrun to the markerand then go to check the source code. For more information, seeHow to: Examine the call stack.
change execution flow
accordingF11ran twice
console.WriteLine
method.The debugger stops at
to send a message
method call, use the mouse to grab the left yellow arrow (run cursor) to move up one line and return toconsole.WriteLine
.accordingF11.
restart the debugger
console.WriteLine
method (you can see it in the output of the console window).By changing the execution flow, you can do things like try different code execution paths or re-run the code without restarting the debugger.
warn
Typically, you should use this feature with caution and you'll see a warning in the tooltip. You may also see other warnings. Moving the cursor cannot restore your program to a previous program state.
accordingF5Go ahead and run the app.
Congratulations on completing this tutorial!
Next step
In this tutorial, you learned how to start the debugger, step through code, and inspect variables. You may want to delve into the debugging features and links for more information.
First look at the debugger
the feedback
Submit and view comments