Change Nothing and Run Again Coder
This browser is no longer supported.
Upgrade to Microsoft Edge to take reward of the latest features, security updates, and technical support.
Make code work in Visual Studio
Applies to:
Visual Studio
Visual Studio for Mac
Visual Studio provides a powerful integrated fix of project build and debugging tools. In this article, find out how Visual Studio tin help you lot find bug in your code using build output, code analysis, debugging tools, and unit tests.
Y'all've figured out the editor and created some lawmaking. Now, you want to make sure the code works properly. In Visual Studio, as with most IDEs, there are 2 phases to making code work: edifice the lawmaking to catch and resolve projection and compiler errors, and running the lawmaking to find run-fourth dimension and dynamic errors.
Build your code
In that location are 2 basic types of build configuration: Debug and Release. The Debug configuration produces a slower, larger executable that allows for a richer interactive run-time debugging experience. The Debug executable should never be shipped. The Release configuration builds a faster, optimized executable that's advisable to ship (at to the lowest degree from the perspective of the compiler). The default build configuration is Debug.
The easiest way to build your project is to press F7, but you lot can also start the build past selecting Build > Build Solution from the primary menu.
You tin can observe the build process in the Output window at the bottom of the Visual Studio UI. Errors, warnings, and build operations are displayed hither. If you accept errors (or if you have warnings above a configured level), your build fails. Y'all tin can click on the errors and warnings to go to the line where they occurred. Rebuild your project past either pressing F7 over again (to recompile only the files with errors) or Ctrl+Alt+F7 (for a clean and complete rebuild).
At that place are two tabbed windows in the results window below the editor: the Output window, which contains the raw compiler output (including error messages); and the Error List window, which provides a sortable and filterable listing of all errors and warnings.
When build succeeds, you see results like this in the Output window:
Review the Error List
Unless you lot've made no modifications to code you've previously and successfully compiled, yous probably take an error. If y'all're new to coding, you probably have lots of them. Errors are sometimes obvious, such every bit a simple syntax fault or incorrect variable name, and sometimes they are hard to understand, with but a cryptic lawmaking to guide you. For a cleaner view of the bug, navigate to the bottom of the build Output window, and click the Error List tab. This takes you lot to a more than organized view of the errors and warnings for your project, and gives you some extra options every bit well.
Click on the mistake line in the Fault Listing window to spring to the line the fault occurs in. (Or turn on line numbers past pressing Ctrl+Q, typing line numbers, and then choosing Plow line numbers on or off from the results. This is the fastest way to become to the Options dialog where you can plough on line numbers.)
Press Ctrl+Grand to quickly jump to the line number where the error occurred.
The error is identified by a red "squiggle" underscore. Hover over it for additional details. Make the set and it will go abroad, although you may introduce a new mistake with the correction. (This is called a "regression".)
Walk through the error list and address all the errors in your code.
Review errors in particular
Many errors may make no sense to you, phrased as they are in the terms of the compiler. In those cases, you'll demand additional information. From the Mistake List window, y'all can do an automatic Bing search for more information on the error or warning. Right-click on the corresponding entry line and select Show Error Help from the context carte, or click on the hyperlinked error code value in the Code column of the Error Listing.
Depending on your settings, either your web browser displays the search results for the error code and text, or a tab opens inside Visual Studio and shows the results of the Bing search. The results are from many unlike sources on the Internet, and not all may be helpful.
Utilize code analysis
Lawmaking analyzers look for common code problems that tin lead to run-fourth dimension errors or bug in code management.
C# and Visual Bones code analysis
Visual Studio includes a built-in set of .NET Compiler Platform analyzers that examine C# and Visual Basic lawmaking as you type. Y'all can install additional analyzers equally a Visual Studio extension, or equally a NuGet package. If rule violations are found, they are reported both in the Fault List and in the lawmaking editor every bit a squiggle under the offending code.
C++ code analysis
To analyze C++ lawmaking, run static code analysis. Go far the habit of running it once y'all've cleaned up the obvious errors that prevent a successful build, and accept some time to address the warnings information technology may produce. Yous'll relieve yourself some headaches down the route, and you may larn a few code style techniques.
Press Alt+F11 (or select Analyze > Run Lawmaking Analysis on Solution from the meridian menu) to start static code assay.
Any new or updated warnings announced in the Error List tab at the bottom of the IDE. Click on the warnings to jump to them in code.
Utilize Quick Actions to set up or refactor code
Quick Actions, available from the light bulb or screwdriver icon, permit you refactor lawmaking inline. They are an easy style to prepare mutual warnings chop-chop and effectively in C#, C++, and Visual Basic code. To access them, correct-click on a warning squiggle and select Quick Deportment and refactorings. Or, when your cursor is on the line with the colored squiggle, printing Ctrl+. or select the light bulb, error light seedling, or screwdriver icon in the margin. Y'all'll run into a list of possible fixes or refactorings you lot can apply to that line of code.
Quick Deportment tin exist used wherever code analyzers determine in that location's an opportunity to prepare, refactor, or improve your code. Click on whatever line of code, right-click to open up the context card, and select Quick Actions and refactorings. If refactoring or improvement options are bachelor, they are displayed. Otherwise, the message No quick actions available here displays in the lower-left corner of the IDE.
With experience, yous can quickly apply the pointer keys and Ctrl+. to bank check for easy refactoring opportunities and clean upward your code!
Run Code Cleanup
Visual Studio provides on-need formatting of your C# code file, including code style preferences, through the Code Cleanup button at the bottom of the editor.
In addition to formatting your file for spaces, indents, et cetera, Lawmaking Cleanup also applies a fix of code style conventions that you lot ascertain. Your preferences for each code style are read from the EditorConfig file, if you have one for the project, or from the code style settings in the Options dialog box.
Debug your running code
Now that y'all've successfully built your code and performed a little clean up, run information technology by pressing F5 or selecting Debug > Start Debugging. This starts your app in a debug environment so you tin observe its behavior in detail. The Visual Studio IDE changes while your app is running: the Output window is replaced by ii new ones (in the default window configuration), the Autos/Locals/Sentry tabbed window and the Telephone call Stack/Breakpoints/Exception Settings/Output tabbed window. These windows take multiple tabs that let you to inspect and evaluate your app'due south variables, threads, call stacks, and various other behaviors as it runs.
Stop your app past pressing Shift+F5 or by clicking the Stop button. Or, you can simply close the app's chief window (or command-line dialog).
If your lawmaking ran perfectly and exactly every bit expected, congratulations! However, if it stops responding, or crashed, or gave yous some foreign results, yous'll need to find the source of those problems and set up the bugs.
Set simple breakpoints
Breakpoints are the almost basic and essential feature of reliable debugging. A breakpoint indicates where Visual Studio should suspend your running code so you lot can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run. You lot don't demand to rebuild a project after setting and removing breakpoints.
Ready a breakpoint past clicking in the far margin of the line where y'all want the break to occur, or press F9 to set a breakpoint on the current line of lawmaking. When you lot run your code, it will pause (or break) earlier the instructions for this line of code are executed.
Common uses for breakpoints include:
-
To narrow down the source of a crash or unresponsive program, scatter breakpoints throughout and around the lawmaking of the method call you think is causing the failure. As y'all run code in the debugger, remove and then reset the breakpoints closer together until you find the offending line of code. Encounter the next section to learn how to run code in the debugger.
-
When you lot introduce new code, ready a breakpoint at the beginning of information technology, and run the lawmaking to brand sure information technology is behaving as expected.
-
If you've implemented a complicated beliefs, set breakpoints for the algorithmic code so y'all can inspect the values of the variables and data when the program breaks.
-
If you're writing C or C++ code, utilize breakpoints to stop the code and then y'all can inspect address values (look for Nil) and reference counts when debugging for memory-related failures.
For more data about using breakpoints, read Using breakpoints.
Inspect your lawmaking at run-time
When your running lawmaking hits a breakpoint and pauses, the line of lawmaking marked in yellow (the current statement) has non executed yet. At this point, you may want to execute the current argument then inspect the changed values. You can use several stride commands to execute lawmaking in the debugger. If the marked lawmaking is a method call, yous can stride into it by pressing F11. You can too step over the line of code by pressing F10. For boosted commands and details on how to footstep through code, read Navigate code with the debugger.
In the preceding analogy, you lot can advance the debugger one statement past pressing either F10 or F11 (since there is no method call here, both commands have the aforementioned result).
While the debugger is paused, you can inspect your variables and phone call stacks to make up one's mind what is going on. Are the values in the ranges you expect to encounter? Are calls beingness made in the right social club?
Hover over a variable to encounter its current value and references. If you lot see a value you didn't expect, yous probably have a issues in the preceding or calling lawmaking. For more in-depth debugging information, larn more than near using the debugger.
Additionally, Visual Studio displays the Diagnostic Tools window, where you can observe your app'south CPU and memory usage over time. After in your app development, yous can use these tools to look for unanticipated heavy CPU usage or memory allocation. Employ it in conjunction with the Watch window and breakpoints to determine what's causing unexpected heavy usage or unreleased resources. For more information, see Profiling characteristic bout.
Run unit tests
Unit tests are your first line of defense against lawmaking bugs considering, when done correctly, they test a single "unit" of code, typically a single role, and are easier to debug than your full program. Visual Studio installs the Microsoft unit testing frameworks for both managed and native code. Use a unit testing framework to create unit tests, run them, and report the results of these tests. Rerun unit tests when you lot brand changes, to exam that your code is even so working correctly. With Visual Studio Enterprise edition, you can run tests automatically after every build.
To become started, read Generate unit tests for your code with IntelliTest.
To learn more about unit tests in Visual Studio and how they tin can help you create amend quality code, read Unit examination nuts.
Run across also
- Beginning look at the debugger
- Learn more virtually using the debugger
- Generate and fix code
Feedback
Submit and view feedback for
grahamtyrdeed1949.blogspot.com
Source: https://docs.microsoft.com/en-us/visualstudio/ide/find-and-fix-code-errors
0 Response to "Change Nothing and Run Again Coder"
Post a Comment