10 tips for Visual Studio debugging: Visual Studio
This article provides 10 debugging skills for Visual Studio, including:
1. Insert breakpoint and breakpoint Management
2. View variable information
3. Shift + F11
4. View stack information
5. Set the next execution statement
6. modify local variables during debugging and continue debugging.
7. Thread Management
8. set conditions for breakpoints
9. Test Method Using the command window
10. Variable history
There is such a Person class:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public bool IsOlderThanOrEqualTo()
{
if (Age >= 18)
{
return true;
}
else
{
return false;
}
}
public void PrintAge()
{
for (int i = 0; i < Age; i++)
{
Console.Write("*");
}
}
public int CalculateDogAge()
{
int dogAge = 0;
dogAge = Age*7;
return dogAge;
}
public void MethodA()
{
MethodB();
}
public void MethodB()
{
MethodC();
}
public void MethodC()
{
}
}
Client call:
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "darren";
p.Age = 22;
int a = 10;
// Within the debugging method
int result = p.CalculateDogAge();
Console.WriteLine(result);
// Debug the bool type
bool olderThan18 = p.IsOlderThanOrEqualTo();
Console.WriteLine(olderThan18);
// Debug the traversal Loop
p.PrintAge();
p.MethodA();
Console.ReadKey();
}
static int sum(int a, int b)
{
return a + b;
}
}
1. Insert breakpoint and breakpoint Management
→ Right-click the code line
→ Move the cursor to any position in a code line, and move the cursor to int result = p. CalculateDogAge ();
→ Right-click, breakpoint, and insert breakpoint
→ A red breakpoint is added to the left of the code line.
● You can also click the leftmost part of the code line to add a breakpoint.
● Multiple breakpoints can be added.
● You can also manage breakpoints through: Debug → window → breakpoint
2. View variable information
→ Move the mouse over the variable name
→ Run the program after a breakpoint is set for the code line, run the program, and stay on the breakpoint line
→ Move the mouse over Variable p and click the plus sign to view the variable information.
You can also click debug> WINDOW> automatic window in the menu bar to view the dynamic information of variables.
You can also click debug → window → local variables in the menu bar to view the dynamic information of all variables.
You can also click debug> WINDOW> monitor 1 on the menu bar, enter a variable name in "Monitor", and view the dynamic information of the variable.
You can also right-click the variable name and click "add to monitoring" or "quick monitoring" to view the dynamic information of the variable.
3. Shift + F11
Use the F11 Statement by statement for debugging. When a method is executed, it will jump to the method.
If you use a process-by-process F10 debugging method, it does not jump to the internal method when you execute a method.
At the same time, Shift and F11 will jump to the upper level of the current execution method.
4. View stack information
You can also view stack information during debugging.
→ Place the breakpoint in int result = p. CalculateDogAge (); line of code
→ Run, staying in the row at first
→ CLICK: Debug → window → call stack
→ When the CalculateDogAge method is not entered, the system calls the pointer pointing to the Main method in the stack window.
→ When you press F11 for statement-by-statement debugging and enter the CalculateDogAge method, call the instance method CalculateDogAge whose Pointer Points to Person in the stack window.
→When F11 is used for statement-by-statement debugging, The CalculateDogAge method jumps out and the pointer in the stack window is called to direct to the Main method again.
5. Set the next execution statement
During debugging, the yellow arrow icon on the left indicates the next line of code to be executed. You can drag the yellow arrow icon to reset the next execution statement of the application.
6. modify local variables during debugging and continue debugging.
During the debugging process, what should I do if I want to modify a variable but do not want to stop this debugging and then generate or debug it? Visual Studio allows you to modify a variable during debugging and continue debugging.
However, this function is not suitable for 64-bit computers. If a 64-bit computer wants to use this function, you need:
→ Right-click the project and click "properties"
→ Click "generate"
→ In "Target Platform", select "x86"
In addition, only local variables can be modified during the debugging process. If you want to modify the class name, method name, and so on, you need to stop the current debugging and then modify it.
7. Thread Management
During the debugging process, click debug → window → thread to view the dynamic information of the thread.
8. set conditions for breakpoints
→ Right-click a breakpoint and click "condition". Enter the following:
That is, when the local variable a is 100, the breakpoint is entered.
→Because the actual value of the local variable is 10, all the variables do not enter the breakpoint and the result is directly output.
9. Test Method Using the command window
Click View> other window> command window, and enter the following:
10. Variable history
Sometimes, you want to record all the values of a variable in the loop traversal process.
class Program
{
static void Main(string[] args)
{
TestMakeObjectId();
}
private static void TestMakeObjectId()
{
List<string> strs = new List<string>();
strs.Add("Hello");
strs.Add("World");
foreach (string str in strs)
{
Console.Write(str + " ");
}
Console.ReadKey();
}
}
→ Place a breakpoint on the Console. Write (str + ""); line code to run.
→ Move the mouse over the variable str, right-click it, and click "add monitoring". The result is as follows:
→ Right-click the row where the str variable is located in the invigilation window and click "create Object ID"
The result is as follows:
→ Start another line in the invigilation window with the name 1 #. The corresponding value is automatically changed to "Hello", which records the value of the variable str in the first time.
→ The value of str changes to "World", and the value of 1 # remains unchanged for statement-by-statement F11. The value of the variable 'str' is recorded throughout the traversal cycle.
References:
10 + powerful debugging tricks with Visual Studio
Advanced Debugging in Visual Studio
Debug a single function with visual studio
Add a breakpoint (F9) to the code that calls the User-Defined Function, and then directly debug (F5). Then, the breakpoint is temporarily stopped, next, you can perform one-step debugging (F10) or go to the function internal debugging (F11 ).
You can also add breakpoints in multiple important locations for direct debugging.
Where can I debug visual studio 2010?
The Red Arrow is the start debugging button, and the pink arrow (box) is the debugging option. Place a breakpoint in the Code and click Start debugging. The debugging option is automatically displayed!