In C#, you are allowed to set certain behavior programmatically and the debugger follows the same during run time. The C# has a unique feature called Attribute that is a declarative tag that is used to convey information to debugger run-time behavior with various elements like classes, methods, structures, enumerators, assemblies in your program. In C#, attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, etc). In this article learn how to use DubuggerHidden attribute declarative that hides methods from debugger in c#.
In the Visual Studio, you are already aware of the benefits of using breakpoints in your code. The code execution will automatically suspend or pause when execution control hits your break points and application goes into debugging mode. Further you will have several options to debug your code easily. In debug mode, you can see the snapshot of objects, call stack, etc. As you have already seen, the call stack contains the trace of all calling methods, exception trace, exception message etc.
Assume that your code contains few methods which just bypass calls to other methods and no other business code is written that help you in debugging. For such methods you expect the debugger not to hit or step in while debugging. However it may decrease in debugging time.
For example, the below code has two break points at method AddTwoNumbers and method Add respectively. In method AddTwoNumbers no additional business logic is written and simply call Add method to calculate addition of two numbers. In this case, typical debugging hit both the break points
In the below video, you can see the normal behavior of the .Net debugger. All the breakpoints encountered are hit in the code while debugging.
If your application is developed using event driven and layered architecture, then you need to take special consideration with respect to how friendly your code is during debugging. In order to build such a debugging environment, you need to write some special code to avoid debugging such methods that makes your debugging activity faster.
Hide methods from debugger in C#:
If any method enclosed with the DebuggerHidden attribute then, Visual Studio debugger will ignore that method during debugging. In this case, if any breakpoints inserted in this method will never hit.
– Article ends here –