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

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.


The DubuggerHidden can be used as follows.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace CalculatorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int nResult = AddTwoNumbers(10, 20);
        }

        [DebuggerHidden()]
        private static int AddTwoNumbers(int nNum1, int nNum2)
        {
            return Add(nNum1, nNum2);
        }

        private static int Add(int op1, int op2)
        {
            return op1 + op2;
        }
    }
}

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

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 –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this: