21. debug our C # program,
The C # program we write won't be able to write right once in many cases. In actual projects, you often need to modify programs and run them properly. There are program errors, mainly in two cases. One is that the program reports an error and cannot run normally. The other is that the program does not report an error, but the execution result of the program is not what we want. Programmers usually call these errors bugs. How can we easily find errors of these programs. Visual Stuido provides debugging functions so that we can easily Debug Programs and locate errors more effectively.
The main debugging functions of Visual Studio include:
1. Output debugging information of some key points.
2. Open a breakpoint. When the program runs to a breakpoint, the program stops.
3. Execute the program step by step.
4. view the values of each variable during running.
Let's take a look at it one by one.
1.Output debugging information of some key points.
You create a project. The solution name is Sloution21, the project name is Exercise21, and the project type is console program. Click Program. cs. Add the code to it.
We can use Debug. WriteLine () to output debugging information. This method is very similar to Console. WriteLine. Console. WriteLine () is to output information to the Dos window. Debug. WriteLine () is the debugging window for outputting information to.
Where is the output window? Click View> output. You will see the output window. Select debug from the drop-down list of the displayed output source. The output debugging window is displayed.
To use Debug. WriteLine (), add a new using statement at the bottom of the using statement. See 6th lines of code.
Using System. Diagnostics;
The Debug. WriteLine () We used is in this library.
We use Debug to output four messages.
1. Start with the main function
2. At the end of the main function
3. 20th rows output user input numbers
4. 23rd rows output the result value after complex operations
Running result
In the debug output window, we will see a lot of output. However, we can find our debugging output in the middle. You can't find it. See my figure.
Using the Debug. WriteLine () method, we can output some key node information of our program to facilitate debugging. In addition, it does not need to interrupt program execution and takes a short time.
2.Breakpoint.
Sometimes we want the program to run a statement and stop it. Just as a child learns to dance, it is necessary to break down complex dances into actions one by one. Stop, we can see more clearly.
It is easy to break a breakpoint. In the gray area on the left of the code line (this is very important and must be in the gray area), click the left mouse button. A red origin is displayed. This red dot is the breakpoint of the program. The program runs here and stops. It is also very easy to cancel the breakpoint, and then click the left mouse button at the same position, the red dot is gone. The breakpoint is canceled. You can create multiple breakpoints at the same time.
Press F5 to run. You will see that the program stops when it runs to the breakpoint. Hover the mouse over the variable and you will see the values of each variable.
3.Step-by-step execution of programs
When the program encounters a breakpoint, the program stops. Next we can let the program be executed step by step.
There are two methods.
L Statement by statement.
Press F11. The program executes the statements one by one. When other methods are called, the program forwards the statements to the called methods for execution one by one.
L process by process.
Press F10. The program treats other methods called as only one statement. When other methods are called, the program will not be transferred to the called method for execution one by one. Others are the same as statement-by-statement execution.
If you want the program to jump directly to the next breakpoint when multiple breakpoints are made in the code, you can press F5 to continue running the program. When a breakpoint is reached, the program stops.
4.View the values of each variable during running.
When we break a breakpoint, we introduce a method to view the values of each variable. Of course there are many other ways to view variables during debugging. I also hope that you can try searching through the network and find other ways to view variable values. If you have any questions, contact me at any time.
The focus of this course is to teach you how to start programming. The sooner the better. Not all knowledge points are exhausted. In addition, I will give you some directions and encourage you to explore the remaining knowledge points on your own. Master the learning methods in this process. The purpose is not to teach.
Exercise
1. Find other methods to view the value of variables during the program running.
2. Debug. WriteLine () outputs the debugging result to the debugging window, which is not very convenient to view because it is mixed with a lot of other information. Check online. How can we output debugging information to a clean window?
3. Try to debug the previously written code. Open a breakpoint and output some debugging information to view the values of various variables during the program running.
4. How can I use F11, f5? Try it several times. It will be frequently used in future programming.
Pay-as-you-go answers to questions C # encountered during the course of getting started.
Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.
HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.
What do you need most at this time?
Yes! A professional teacher will help you!
If you have any questions during this course, you can ask the author for help via qq or email.
Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.
The payment method is very simple:
Scan the following QR code. Leave your QQ number or email address in the payment message.
Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.
Instructor qq: 2040227296
Instructor email: 2040227296@qq.com