How to hide methods from debugger in C# using DebuggerHidden attribute

Debugging Debugging Best Practices How to guide

In C#, you are allowed to set certain behavior pro-grammatically and the debugger follow the same during run time. The C# has a unique feature called Attribute that is a declarative tag 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 have already aware benefits of using break points in your code. The code execution will automatically suspended or paused when execution control hit 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 call 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 stepping 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 written and simply call Add method to calculate addition of two numbers. In this case, typical debugging hit both the break points
Hide methods from debugger in C#

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 during debugging. In order to build such debugging environment, you need to write some special code to avoid debugging such methods that makes your to debug 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 break points inserted in this method will never hit.

The DubuggerHidden can be used as follows.

The DubuggerHidden initialize a new instance of Systems.Diagnostics.DebuggerHiddenAttribute class. The debugger skip stepping into method AddTwoNumbers and the break point will not hit. But, you can see the AddTwoNumbers in the stack trace since it is the part of the debugging flow.

Hide methods from debugger in C#

In the below video you can see during debugging how debugger ignore the method AddTwoNumbers and skip the break point after enclosing the DebuggerHidden attribute.

– Article ends here –

Related Tags:

Software engineer by profession, founder of troubleshottyourself channel and enthusiastic blogger. Love to write articles on new technology, programming,

Share this: