Sunday, November 27, 2005

Using DebuggerStepThroughAttribute

You can use System.Diagnostics.DebuggerStepThroughAttribute to prevent the Visual Studio debugger from stopping at the method/property call when you step through you code.

For example, you have a program like this :
1using System;
2using System.Diagnostics;
3
4namespace DebuggerStepThrough_Demo
5{
6 public class Employee
7 {
8 public Employee()
9 {
10 _name = "";
11 _salary = 0;
12 }
13
14 public Employee(string name, double salary)
15 {
16 _name = name;
17 _salary = salary;
18 }
19
20 private double _salary;
21 public double Salary
22 {
23 set { _salary = value; }
24 get { return _salary; }
25 }
26
27 private string _name;
28 public string Name
29 {
30 set { _name = value; }
31 get { return _name; }
32 }
33
34 public double IncreaseSalary(double amount)
35 {
36 Salary += amount;
37 return Salary;
38 }
39 }
40
41 class Class1
42 {
43 [STAThread]
44 static void Main(string[] args)
45 {
46 Employee emp = new Employee("My Name", 1000);
47
48 emp.IncreaseSalary(emp.Salary * 0.5);
49
50 }
51 }
52}
53



When you put a breakpoint at the line emp.IncreaseSalary(emp.Salary * 0.5);, and you step through the code in the debugger, the debugger will step into the get method of Salary property before the IncreaseSalary method. You can stop this by using the DebuggerStepThroughAttribute attribute.


1        public double Salary
2 {
3 set { _salary = value; }
4
5 [DebuggerStepThrough()]
6 get { return _salary; }
7 }
8



Now, when you step through the code again in debugger, the debugger will step into IncreaseSalary right away.

This is useful if your property getter and setter does not perform complex situation and you don't want
to step into those code during debugging.

Labels:

0 Comments:

Post a Comment

<< Home