Write simple Debug helpers to help you debug and maintain your code

Much of my time is spent using the Visual Studio debugger examining code to figure out how it works and how to fix it. When stepping through a function, the values the function uses are very useful for code understanding. The debugger shows these values in the Watch/Locals/Auto/Callstack windows.  For example, the Locals window shows the Local variables used by the current code while at a breakpoint. Each variable might be a structure or class with many members. You can click to expand to examine sub-members of a value.


I also like to modify code while the debug session is live, so I can see real values while writing. Edit and Continue is a very useful feature, but it doesn’t work in every case, such as creating new methods or types. Also, native code doesn’t support EnC. So I like using an external editor (perhaps another instance of VS) to modify the code. This means that your debug session is still live, and the source code file still matches the binary, until you save the file in the external editor.


The string shown for a variable can be customized to help understand what the code is doing. As you step through the code, if you can understand at a glance what the function is doing, rather than drilling into sub-members, you’re more efficient. Some types have dozens of variables, and some might be more important than others.


For native code (C++, C) you can customize the display string with this: Customize the VS debugger display of your data


For managed code (VB, C#) you can override the ToString method to customize the string. In VS 2008, the debugger will display the results of calling the ToString method.


There are 2 options:


Enable Property Evaluation and other implicit function calls

Call string conversion function on object in variables windows (C# and javascript only)


The next version of Visual Studio will add VB to this feature!


As a simple workaround, to have the debugger call ToString in VB programs, you need only prefix your types with

<DebuggerDisplay(“{tostring}”)> _


The line continuation character won’t be needed in the next version either!


If you don’t have the source code or can’t modify it, you can still modify the displayed string: Customize the display of types in the Debugger using Extension Methods and DebuggerDisplay Attribute


Below are a VB and C# program demonstrating how the string can be customized.


Start VS2008, choose File->New->Project->C# or VB->WPF Application, then paste in the code.


Put a Breakpoint on the indicated line and hit F5. You’ll see the string displayed is the result of the ToString function.



See also:

Use temporary projects in Visual Studio

Customize the display of types in the Debugger using Extension Methods and DebuggerDisplay Attribute

Customize the VS debugger display of your data

New Features in Visual Basic 10

Remove double spaces from pasted code samples in blog

Make your code more maintainable: The evils of the Return statement

Very Advanced Debugging tips




Class Window1

    Sub dd() Handles Me.Loaded

        Dim y = New TestClass

        Dim r = “Put a bpt here”

    End Sub

End Class


<DebuggerDisplay(“{tostring}”)> _

Class TestClass

    Dim x = “asdf”

    Public Overrides Function ToString() As String

        Return “here we are in tostring “ + x

    End Function

End Class




Here’s the C# equivalent Program



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;


namespace WpfApplication1


    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window


        public Window1()



            var x = new TestClasss();

            var y = “set a bpt here”;






class TestClasss


    String foo = “test”;

    public override string ToString()


        return “i’m in the test class “ + foo;







Skip to main content