.NET debugging made easier

.NET debugging made easier

Rate This
  • Comments 16

Not sure about you but I was not aware of the existence of the DebuggerStepThroughAttribute. Debugging code can be difficult at times and any tool or mechanism that can ease this pain is always welcome.

As far as the CLR is concerned, there is no semantic attached to this attribute. However Visual Studio does not step through methods or classes that are decorated with this attribute. Although you could still use breakpoints in those methods or classes, they are never hit.

And here is how breakpoints would look in Visual Studio 2008:

 

This attribute can be applied to methods, property accessors, classes and structs. I found it to be extremely useful when I wanted to step through a method without first stepping through all property accessors used as parameters or the source instance of the method. In the example below, a.Value and b.Value are not stepped through when pressing F11 in Visual Studio. It is only the Add method that is fully debugged:

var a = new SomeValue(10);

var b = new SomeValue(150);

...

Add(a.Value, a.Value);


class SomeValue

{

  ...

 

  public int Value

  {

    [DebuggerStepThrough]

    get { return value; }

  }

}

Enjoy!

Leave a Comment
  • Please add 8 and 5 and type the answer here:
  • Post
  • PingBack from http://wordnew.acne-reveiw.info/?p=11003

  • I am a big fan of this attribute.  If you want to stop at a breakpoint set in a method decorated with this attribute there is an option.  In visual studio 2008 (I believe it is in the same place in 2005) under Tools | Options | Debugger | General uncheck "Enable Just My Code (Managed Only)" and your breakpoint will still trip.  If you attempt to step into the same method without the breakpoint you will still glide over.

  • Thanks very much, this will speed up debugging considerably

  • I read about this moons ago.  Then when I got fed up with the debugger stepping over all the properties I I thought I should give it a try.  Alas, I could not remember it.  I asked about but did not really get the answer I was looking for.  Thanks for bringing it up.

  • I don't really like the fact that you have to flag each property that you want to skip.  There should be some other directive to just skip all property accessors.

  • This might be new to 2008 or even sp1 (I dont remember seeing it in 2005) but the context menu for the code window now has two items during a debug session that can help with just this sort of thing. The first is a "Step Into Specific" submenu that shows each of the method calls made by the current line. The other is a checkable "Step over properties and operators" option that does just as the name suggests (VS prints a message in the output window if you step over a property with it enabled). I'd try using them before the attribute, simply because you don't have to modify the code just for debugging (or remember to remove it when you're done).

    Hope it helps

  • I have also removed these attributes to aid debugging. (The designers add these by default)

    Schneider

  • I just remembered that the c# preprocessor directives "#line hidden" and "#line default" can be used to produce the same effect as this attribute, the debugger will treat any code between the two directives as if it doesn't exist. Again, I wouldn't do this without a really good reason, but it can be useful when creating a code generator that needs to hide some mess from the developer at debug time.

  • Should it not be called DebuggerStepOver though? It is technically not stepping through that code.

  • thanks for sharing. That is so useful

  • this attribute is basically ideal for custom debugger helper methods such as custom assertion methods, exception handling methods, etc. etc. etc. as well as source code generation for which you don't want get the hell confused out of consumer developer...

    anyway, by VS2008 you've also got the <b>Debugger\Step over properties and operators (Managed only)</b> option which I personally most of the time make it disabled :D

  • NetBeans can step over and into within a line, very nice.

  • Art is the process or product of deliberately arranging elements in a way that appeals to the sense or

  • Art is the process or product of deliberately arranging elements in a way that appeals to the sense or

  • More like DebuggerStepOverAttribute than step through.

Page 1 of 2 (16 items) 12