Continuing on with an exploration of the various debugger attributes found in the System.Diagnostics namespace, this week I’d like to present the DebuggerHidden attribute.

This is something you probably only want to apply to VERY well tested code. Code marked with the DebuggerHidden attribute will be stepped over during debugging, and the method call will not even appear in the Call Stack window. However, if some code marked with this attribute makes a call to another method, the debugger steps into that method.

It’s also worth noting that any breakpoints that have been set in code marked with the DebuggerHidden attribute will be ignored. For example, in the following code snippet, a breakpoint can be set in both ClickHandler and NotSoHiddenMethod:

private void ClickHandler(object sender, EventArgs e)
{
   HiddenMethod();
}

[DebuggerHidden()]
public void HiddenMethod()
{
   Console.WriteLine("Can't set a breakpoint here");
   NotSoHiddenMethod();
}

public void NotSoHiddenMethod()
{
   Console.WriteLine("Can set a breakpoint here!");
}

If you step through this code, the debugger goes from the call to HiddenMethod in the ClickHandler method straight to the NotSoHiddenMethod. The call stack at this point is shown below, and you can see that HiddenMethod does not appear in the stack.

Call stack showing the effect of the DebuggerHidden attribute

As with all of the System.Diagnostic attributes, the CLR will ignore this, so you will still see the method call in the stack trace of any an exceptions thrown at runtime.