How to Hide Methods from debugger Using DebuggerHidden attribute

In this post we will learn “How to Hide Methods from debugger Using DebuggerHidden attribute“. The DubuggerHidden property tells the Visual Studio debugger that this method is hidden from and during debugging. This is useful when you don’t want to enter a method while debugging.

When you mark a method with DebuggerHidden() attributes, it will explicitly tell the debugger not to step into those methods and no breakpoints will occur on that method. Now I will explain the same using an example of Intellitrace debugging.

How to Hide Methods from debugger Using DebuggerHidden attribute

Let’s consider you have below code snippet

How to Hide Methods from debugger Using DebuggerHidden attribute

We have set two break points in each of the method (Line 22, and Line 31 as shown). Let’s run this application and check the output intellitrace debugging window, you will get bellow output as shown below.

How to Hide Methods from debugger Using DebuggerHidden attribute

According to the image above, you can see the application flow from Main() > Method1() > Method2(). If you also select the “Call Stack” of the current live event, this also represents calling method2() from method1(). This is consistent with our expected behavior.

Now, if you do not need to enter Method1() while debugging and the debugger does not need to stop inside Method1() for any breakpoint, you need to add “DebuggerHidden()” attribute with method1() . If you try to debug an application with the same breakpoint, the CallStack() and IntelliTrace debugger views will be different.

How to Hide Methods from debugger Using DebuggerHidden attribute

If you check the above image, the debugger reached Method 2() through Method 1(), but the breakpoint in method 1() was never reached due to the “DebugerHidden” attribute.

If you switch from “EventView” mode to “CallView” mode in the Intellitrace window, you can find the actual flow of the program thread.

How to Hide Methods from debugger Using DebuggerHidden attribute

I hope above will help. You can check more articles on our website under C# section HERE or you can also check same on Microsoft Docs too.

Leave a Reply

Your email address will not be published. Required fields are marked *