64

I quickly want to determine whether/when a set of events are triggered. Therefore I quickly assigned empty lambda's to them.

_session.TopologyStarted += () => { };

When tracing through the code when the event is triggered, the debugger goes into the anonymous function, allowing to debug it's execution. However, when setting a breakpoint on this line of code, a breakpoint is set on the assignment of the anonymous function to the event.

Is there a way to set a breakpoint within the anonymous function?

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161

1 Answers1

110

Put your cursor in the anonymous function (between the {}'s) and press F9.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • 23
    After that many years of using Visual Studio you'd think you'd know it all. :) I feel so stupid now, thanks! – Steven Jeuris Sep 06 '12 at 13:17
  • 3
    When placing a breakpoint within empty brackets, you need to place the cursor right after the opening '{' for the breakpoint to be placed within the anonymous method. – Steven Jeuris Sep 06 '12 at 14:30
  • for some reason it doesn't work for me on a Lazy object - any ideas? – developer82 Nov 29 '13 at 18:36
  • Isn't this by itself enough of a reason to use anonymous methods vs lambdas? – dashnick Feb 16 '17 at 17:22
  • 1
    @dashnick Nope, you can do it as well using lambda expressions - working at least in my VS17RC ... Just place the cursor next to the lambda operator `=>` and hit F9. – KnorxThieus Mar 02 '17 at 17:44
  • 2
    Keep in mind that this [wont work on Expressions](https://stackoverflow.com/questions/42799704/) expressed as delegates – Daniel Park Sep 28 '17 at 21:43
  • A note to anyone else that doesn't have f-keys (stupid macbook touchbar): you can also right-click > breakpoints > insert breakpoint. – Steven Evers Dec 16 '19 at 00:05
  • can't get this to work, either. The breakpoints are fine, but still don't get hit. – MC9000 Apr 16 '20 at 02:26
  • I am able to place a breakpoint within the anonymous function but the breakpoint is not getting hit during the debug. Any suggestions on how the breakpoint could be hit? – Carthic Apr 21 '22 at 02:07