Tagged by '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();
    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. :-)