We write the C # program, many times not can write right at once. Especially in the actual project, you often have to do is to constantly modify the program you write, let it run normally. There are two main cases of error in the program. One is the program error, can not run normally. The other is that the program does not have an error, but the execution result is not what we want. Programmers often call these bugs a bug. How can it be easier to find errors in these programs? Visual Stuido provides debugging features that allow us to compare easy Debug programs to find the wrong place more efficiently.
The main debugging features of Visual Studio include these:
1. Output debugging information for some key points.
2. Break point. When the program runs to a breakpoint, the program stops
3 Step-by-step program execution.
4. View the values of each variable during the run.
We'll learn and learn about each other next.
1. output debugging information for some key points.
You create a project. The solution name is SLOUTION21, the project name is EXERCISE21, and the project type is a console program. Click Program.cs. Add the code to the inside.
/span>
We can use Debug.WriteLine () to output our debug information. This method is very similar to Console.WriteLine (). Console.WriteLine () is the output of information to a DOS window. Debug.WriteLine () is the output debug window that outputs information to vs.
Where is the Output window? Click View-"output. You will see the Output window. Select Debug in the drop-down list that shows the output source. You see the Output Debug window.
To use Debug.WriteLine (), we first add a new using statement at the bottom of the using statement. See the 6th line of code.
Using System.Diagnostics;
We used the Debug.WriteLine () in this library.
We used debug to output 4 messages.
1. At the beginning of the main function
2. At the end of the main function
3. line 20th outputs user-entered numbers
4. Line 23rd outputs the value of result after a complex operation
Run results
We'll see a lot of output in the Debug Output window. But in the middle we can find our debug output. You didn't find it, just look at my picture.
Using Debug.WriteLine () This method, we can put some key nodes of our program information output to facilitate our debugging. And it takes a short time to execute without interrupting the program.
2. break point.
Sometimes we want the program to run to a statement and stop. Just like a child learns to dance, a complex dance is broken down into a movement. Stop, we can see more clearly.
The break point is simple. In the left gray area of the line of code (which is very important, it must be within the gray area), click the left mouse button. A red Origin point appears. This red dot is the program's breakpoint. The program will stop running here. It is also very easy to cancel breakpoints, then click the left mouse button in the same position, the red dot is gone. The breakpoint is canceled. Can hit multiple breakpoints at the same time oh.
Press F5 to run. You will see the program, and run to the breakpoint where it stops. Hover over the top of the variable and you'll see the values of each variable.
3. Let the program step-by-step Execution
When the program hits a breakpoint, the program stops. Then we can get the program to execute step-by-step.
There are two ways.
L statement.
Press the F11 key. The program executes each statement, encounters calls to other methods, and the program goes to the called method.
L process.
Press the F10 key. The program will call other methods just as a statement. When a call to another method is encountered, the program does not go to the method that is called. The other is the same as per-statement execution.
When you hit multiple breakpoints in the code, you want the program to jump directly to the next breakpoint, you can press the F5 key to keep the program running. When you hit the next breakpoint, the program stops.
4. View the values of each variable during the run .
At the point of interruption we introduced a way to view the values of individual variables. There are, of course, many other ways to view variables during debugging. I also hope that you can try to find out through the network, try to find other ways to see the value of the variable. You can always consult me if you have any questions.
I'm talking about this course, the point is to teach you to start programming, the faster the better. Not to exhaust all knowledge points. There is, I will give you some direction, encourage you to explore the rest of their own knowledge points. Master the way of learning in this process. Teaching is to not teach.
Practice
1. Find other ways to view the values of variables during program run.
2. Debug.WriteLine () output debug results to the Debug window, mixed with many other information, is not very convenient to view. Check online, how to let our debugging information output to a clean window?
3. Try to debug the code you wrote before. Break point and output some debugging information to see the values of each variable during program run.
4. When F10,f11,f5 debugging, how to use? Try it for yourself a few times. It is often used in future programming.
Pay for answers to the various issues encountered in the C # Introductory learning process.
0 Basic Learning C # Getting started, most of the problems can be solved by self-study or Internet.
but there will be some problems, not so easy to solve, become a stumbling block of your study. At last it even caused you to study halfway. Often you get started with C # programming, and you'll encounter 20 to 30 more issues that you can't easily solve.
At this time, what do you need most?
Right! A professional teacher to help you!
When you are studying this course, you can ask the author of this tutorial for help by QQ or email if you encounter any problems.
With just $30 , you can get three professional answers from the author of this tutorial. So you can complete the C # entry with less than 300 yuan and push the software industry's door open.
The payment method is very simple:
Scan the QR code below . Leave your QQ number or email address on the payment message.
Education is not to be cheap, but to be reliable! Really smart people, more know how to spend money to let professional people serve their own, improve their efficiency.
Teacher qq:2040227296
Teacher Email:[email Protected]
21 Debugging our C # program