DebuggerHidden Attribute and Other Cool Debugging

Debugging a page that uses many methods from other classes can become a right pain in the neck. I find myself accidentally stepping into a method that I don’t need to debug or wanting to output specific values from my methods straight away. Being the cowboy (correction, agile) developer that I am, one of my code boffins at work showed me a two cool ways to meet my debugging needs:

1) DubuggerHidden Attribute

Using the DubuggerHidden attribute tells the Visual Studio debugger that the method is hidden from the debugging process. The simple example below, shows the DebuggerHidden attribute in use:

protected void btnDoSomething_Click(object sender, EventArgs e)
{
//Output random number to Textbox
txtOutput.Text = GetNumber(1, 10).ToString();
}

[DebuggerHidden]
int GetNumber(int min, int max)
{
System.Random random = new Random();
return random.Next(min, max);
}

2) DebuggerDisplay Attrubute

The DebuggerDisplay attribute allows us to output variable values from a class or method to be displayed in our Visual Studio debugger. The attribute includes a single argument that is supplied as a string. The string can contain references to fields, properties and methods in the class so that the actual values from an object may be included in its description.

[DebuggerDisplay("a={a}, b={b}, ValueTotal={CalculateValues()}")]
public class AddValues
{
public int a { get; set; }
public int b { get; set; }

public int CalculateValues()
{
return a + b;
}
}

You also have the ability to include simple expressions, like so:

[DebuggerDisplay("a={a}, b={b}, ValueTotal={CalculateValues() * 2}")]

In addition to the “DebuggerHidden” and DebuggerDisplay attributes, .NET contains some very useful attributes that modify the behaviour of a debugger. For me, they weren’t as interesting as the two debugger attributes I listed above. :-)

blog comments powered by Disqus

About

Surinder Bhomra is a Web Developer.

He has achieved a BSc in Information Systems in 2006 and since then has been working in the IT industry.

Prior to working in the Web Development industry I have spent 1.5 years working as an IT Systems Analyst providing support for internal company systems.

Working in the Web Development industry has given me the opportunity to expand my current skills and allowing me to work on website projects using ASP, ASP.NET, CSS, HTML and SQL.

StackOverflow Flair

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

>