Visual Studio Debugging Tips and Tricks

In the “Hour of Power” presentation at Tech.Ed, I showed how Visual Studio 2008 could be used for a better Debugging Experience. I tried to showcase 10 tips and mentioned that I’ll update the details of it in my blog. Here it is-

1. Debugging/Stepping into Framework code

With the launch of Visual Studio 2008, Microsoft have allowed developers to debug into the .NET Framework code.  This doesn’t happen by default however, and some settings need to be configured. The steps involved in configuring this for Visual Studio 2008 SP1 are:

  • Go to Tools->Options menu item and select Debugging/General on the left side of the dialog
  • Ensure that Enable .NET Framework source stepping option is checked
  • Ensure that Enable source server support option is checked
  • Select Debugging/Symbols on the left side of the dialog
  • Add the Symbol file location symbols
  • Specify a cache symbol location directory



Once this is set, you will be able to step into .NET Framework code, set break points in it and look at values of private data members.  This not only provides the ability to debug the .NET Framework source code, it also provides a great reference implementation of classes.

2. Moving the Execution Point

When you are stepping through code in Visual Studio, the IDE displays the yellow arrow on the line of the code that it is about to execute.


This yellow arrow can be dragged and dropped on to any line that needs to be the execution line. Although, dragging the code forward can have some undesirable consequences if variable declaration and assignments are skipped, this feature can be very useful if used properly.

3. Step into Specific

In older Visual Studio versions, while stepping into code that has more than one function in the same line, the debugger ends up stepping into and out of each function. This can be very annoying at times. Visual Studio 2008 provides a functionality called Step Into Specific. This can be accessed from the context menu while debugging and allows you to step specifically into the function that you are interested in.


4. Step over properties and operators

Another annoying thing in older versions of Visual Studio was that the debugger would step into properties and operators. In a lot of cases, the properties were just wrappers for private data members, and stepping in and out of these properties was not particularly useful. One way to get around this problem is to decorate the property with the attribute DebuggerStepThrough. This would prevent the debugger from stepping into the property.

Visual Studio 2008 provides a much better debugging experience by providing an option called Step over properties and operators, that allows you to chose whether you would like to step into the property’s source code or not. This option can be set from the Options dialog -


It is also available in the context menu that pops up while debugging -


5. Tracepoints

Visual Studio 2008 allows developers to easily add Tracepoints to look at values while debugging. This feature was also available in older versions of Visual Studio, but accessing this has just become a whole lot easier. So, what are Tracepoints? They are breakpoints, that allow you to execute something like printing a trace message on the Debug output window and optionally continue the execution without stopping when the break point has been hit. Trace points can be enabled from the debug context popup menu as shown below:


The variables whose values are to be displayed in the output window will have to be enclosed in curly brackets as shown in the screen shot below.


6. Conditional break points and trace points

Visual Studio also allows you to add conditional break points and trace points that are hit only when a certain condition is met. This can be easily accessed by right-clicking on a break point glyph


As shown in the menu options, you can specify the condition, hit count or even a filter for the break (or trace) point.


7. The DebuggerDisplay attribute

When inspecting values in the debugger, Visual Studio does a great job of displaying the values just by hovering over a variable. But when you are inspecting objects, it is not always helpful as this example shows:


You will have to drill further into the object to see what the values are. To get over this problem the class Book can over ride the implementation of ToString. The Debugger will then display this value when it is inspected.

However, this is not the right way of doing it. The right way of doing it is to decorate the class with the DebuggerDisplay attribute as shown below -

class Book
    public string ISBN { get; set; }

    public string Title
        get { return title; }
        set { title = value; }

Anything displayed within the curly brackets in the Attribute string will be evaluated - in this case the two properties ISBN and Title will be expanded as shown below -


This is definitely an improvement, but it still needs a bit of clean up as the two strings show quotes and null is being displayed when ISBN is not present. To accomplish this, the DebuggerDisplay attribute can be changed to -

[DebuggerDisplay("{ISBN == null ? \"\" : ISBN + \": \",nq} {Title.Substring(0, System.Math.Min(Title.Length, 33)) + (Title.Length > 33 ? \"…\" : \"\"), nq}")] 

class Book

The nq option within the curly brackets is for no quotes. In addition, expressions can also be evaluated within the braces. This is used to to display an empty string if ISBN is null and also to display elipsis when the length of the book title exceeds 33 characters. The net result is something like this -


8. The DebuggerBrowsable attribute

If we expand the first object in the array from the example above, we will notice that the Title is shown twice -


This happens because the lower case "title" is a private data member that the property "Title" uses. In effect, they are both the same, but if we have a class that has a lot of private data members that are wrapped by properties, a lot of redundant information will be displayed. To prevent certain data members to not be displayed, they will have to be decorated with a DebuggerBrowsable attribute with the DebuggerBrowsableState.Never set as shown below -

 9. The DebuggerTypeProxy attribute

When the variable values are displayed in the debugger, Visual Studio automatically evaluates all data members and properties and displays them on the screen. The DebeuggerTypeProxy attribute allows an alternate class to be used to determine which data members and properties are to be displayed. In the example shown below, the proxy class even calls a method, which is not evaluated by the debugger -

[DebuggerDisplay("{ISBN == null ? \"\" : ISBN + \": \",nq} {Title.Substring(0, System.Math.Min(Title.Length, 33)) + (Title.Length > 33 ? \"…\" : \"\"), nq}")] 
class Book

class ProxyView
    Book book;
    public ProxyView(Book book)
    { = book;

    public string ConverToXml
	    //Exposing method as a property	
            return book.ConvertToXml();


    public string Title
            return book.Title;

    public string ISBN
            return book.ISBN;

10. The System.Diagnositcs Namespace

The System.Diagnostics class also contains a number of classes that can be used for printing out trace or debug information. The class Debug is used while compiling and running the application in Debug mode and the output is displayed in the Output window in VS 2008. The syntax for the Trace class is very similar to that of the Debug class with the exception that it will still be available while compiling and running the application in Release mode.

In the sample code shown below, a ConsoleTraceListener is added to redirect the traces to print the message to the console, rather than the output window

        Trace.Listeners.Add(new ConsoleTraceListener());

        Debug.WriteLine("[My Debug message]");
About these ads

~ by blogesh on September 9, 2008.

29 Responses to “Visual Studio Debugging Tips and Tricks”

  1. Great post!

    I picked up quite a few things from your “Hour of Power” presentation, and good to now have everything in a blog post. Specially the “trace point” was new to me, and something I can have great use of!


  2. Thanks, Jonas.

    Your presentation was great too and looking forward to seeing your Silverlight presentation again at Victoria .NET next month – it was the most comprehensive end-to-end presentation of Silverlight I’ve seen so far.

  3. BTW – I should have mentioned that things like Step into Specific and the option to Step over properties and operators are only available in VS 2008 Service Pack 1.

    In addition, With SP1 you should be able to load the Symbols directly from the Microsoft Symbol server without explicitly specifying the source server location.

  4. I love this post.
    Thanks buddy.

  5. Hi Mahesh!

    Do you possibly know how many lines can have a file for being debugged with VS2008??

    I work with C and it is possible to find BIG files, with more than 100000 auto-generated lines by another system, so I think I pass this maximum number

  6. Hi Carlos,

    I am sure VS 2008 can handle even 2GB files (don’t know the exact limit, though) – but peformance may not be that good. I think you should really try and address the real problem – which is having a really large file. Can’t you just split the auto-gen file to more manageable chunks?

  7. [...] Visual Studio Debugging Tips and Tricks [...]

  8. Thanks for the great post.

    Although i’ve always considered that I have a pretty good handle on the debugger (better than the average – probably because my code is buggier than average), I found great new ways to do things here.

    Nice work.

    – b#

  9. Great post, thank you. I personally somehow missed the Tracepoints altogether.

    – Phil

  10. [...] Visual Studio Debugging Tips and Tricks Posted: Friday, October 24, 2008 9:59 AM by jwiese [...]

  11. Thanks for collecting all in one place!

  12. On the different boat, I’ve found my VS.Net were slowing down and down when changing from design view to code view. And also when setting up the properties (naming the control, etc). I’m using VS.Net 2008. Thanks for the solutions before, I’m waiting :)


  13. It is quite useful tips, thanks for your effort!


  14. Found really useful, especially the settings for properties and operators.

    Thank you.

  15. This works real well…thanks

  16. [...] by Mahesh Krishnan on [...]

  17. Thanks! This is very useful information. keep up the good work!

  18. If you don’t mind I’m going to add this post and your blog to my delicious favorites so I can share it with my family. I appear forward to coming back to read your future posts as well.

  19. A semen fluid volume analysis evaluates certain characteristics of a male’s low semen and the sperm amount found in the semen fluid volume. It may be done while investigating a couple’s infertility problem or after a vasectomy to check that the procedure was successful. It is also used for testing the donors for semen fluid donation. Nowadays it is really possible to produce more semen with completely safe ways like taking natural pills from the various Internet stores.

  20. There are actually plenty of details like that to take into consideration. That could be a nice level to deliver up. I provide the ideas above as normal inspiration but clearly there are questions just like the one you convey up where an important thing will likely be working in honest good faith. I don?t know if best practices have emerged around things like that, however I am certain that your job is clearly identified as a good game. Each boys and girls feel the impression of only a moment’s pleasure, for the remainder of their lives.

  21. I have meant to put in writing about a little something like th is on my blog site and th is gave me an thought. TY.

  22. I’m really thankful to the author with this post to make this lovely and informative article live to put us. We really appreciate ur effort. Continue the nice work. . . .

  23. Your internet site is definitely among the greatest . Overall perception of the webpage is simply stupendous .
    Miramar CA Locksmith

  24. I just really enjoy what we should post right here. Particularly new not to mention wise. A single concern though. I’m running Firefox with the help of Debian not to mention components with the existing layout products would be a bit wonky. I just know it’s not really popular developed. But it’s something towards hold in the mind. I just wish that going barefoot will probably guidance not to mention keep the best good penning.

  25. hybrid tattoo machine,

  26. Hum, this is real nice.This topic interest me and this post has enlightened more.

  27. [...] usati rolex watch rolex watch for sale rolex watch prices rolex watches rolex watches for men rolex watches prices rolex watches used rolex webshop rolex yachtmaster Click here to cancel [...]

  28. A cool post there mate ! Cheers for the post .

  29. Great tips!
    thank you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: