Note that there are some explanatory texts on larger screens.

plurals
  1. POVisual Studio debugger tips & tricks for .NET
    primarykey
    data
    text
    <p>I've been working for years with VS's debugger, but every now and then I come across a feature I have never noticed before, and think "Damn! How could I have missed that? It's <strong>so</strong> useful!"</p> <p>[Disclaimer: These tips work in VS 2005 on a C# project, no guarantees for older incarnations of VS or other languages] </p> <h3>Keep track of object instances</h3> <p>Working with multiple instances of a given class? How can you tell them apart? In pre-garbage collection programming days, it was easy to keep track of references - just look at the memory address. With .NET, you can't do that - objects can get moved around. Fortunately, the watches view lets you right-click on a watch and select 'Make Object ID'. </p> <blockquote> <blockquote> <p><a href="http://img403.imageshack.us/img403/461/52518188cq3.jpg">watches view http://img403.imageshack.us/img403/461/52518188cq3.jpg</a> </p> </blockquote> </blockquote> <p>This appends a {1#}, {2#} etc. after the instance's value, effectively giving the instance a unique label. It looks like this: </p> <blockquote> <blockquote> <p><a href="http://img383.imageshack.us/img383/7351/11732685bl8.jpg">numbered instance http://img383.imageshack.us/img383/7351/11732685bl8.jpg</a></p> </blockquote> </blockquote> <p>The label is persisted for the lifetime of that object.</p> <h3>Meaningful values for watched variables</h3> <p>By default, a watched variable's value is it's type. If you want to see its fields, you have to expand it, and this could take a long time (or even timeout!) if there are many fields or they do something complicated.</p> <p>However, some predefined types show more meaningful information :</p> <ul> <li>strings show their actual contents</li> <li>lists and dictionaries show their elements count etc.</li> </ul> <p><a href="http://img205.imageshack.us/img205/4808/37220487md1.jpg">meaningful info http://img205.imageshack.us/img205/4808/37220487md1.jpg</a></p> <p>Wouldn't it be nice to have that for my own types? </p> <p>Hmm... </p> <p>...some quality time with .NET Reflector shows how easily this can be accomplished with the <code>DebuggerDisplay</code> attribute on my custom type:</p> <pre><code>[System.Diagnostics.DebuggerDisplay("Employee: '{Name}'")] public class Employee { public string Name { get { ... } } ... } </code></pre> <p>... re-run, and...</p> <p><a href="http://img60.imageshack.us/img60/926/79816018ha1.jpg">ta da! http://img60.imageshack.us/img60/926/79816018ha1.jpg</a></p> <p>There's a lot more info on the subject here: <a href="http://msdn.microsoft.com/en-us/magazine/cc163974.aspx">MSDN</a></p> <h3>Break on all exceptions</h3> <p>... even the ones that are handled in code! I know, I'm such a n00b for not knowing about this ever since I was born, but here it goes anyway - maybe this will help someone someday:</p> <p>You can force a debugged process to break into debug mode each time an exception is thrown. Ever went on a bug hunt for hours only to come across a piece of code like this?</p> <pre><code>try { runStrangeContraption(); } catch(Exception ex) { /* TODO: Will handle this error later */ } </code></pre> <p>Catching all exceptions is really handy in these cases. This can be enabled from <em>Debug > Exceptions... (Ctrl-Alt-E)</em>. Tick the boxes in the 'Thrown' column for each type of exception you need.</p> <hr> <p>Those were a few forehead-slapping moments for me. Would you care to share yours?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload