30

I was trawling through some of the IL of one of my assemblies (via ILDasm) and I noticed that all of my methods begin with a nop instruction.

Does anyone know why that is?

YellPika
  • 2,872
  • 2
  • 28
  • 31

1 Answers1

45

The assembly was compiled in debug mode. Nop instructions do not do anything (i.e have no side effects), but act as a convenient instruction to place a breakpoint.

Tip

If you need a place for an additional breakpoint for debugging purposes, you can force the inclusion of a Nop in a Debug build by adding a pair of empty braces, e.g.

_grid.PreviewMouseRightButtonDown += (sender, e) =>
{
    _isRightMouseDown = true;

    RowColumnIndex cell = _grid.PointToCellRowColumnIndex(e);
    {} //<------ Adding a Nop allows a breakpoint here.
};
Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • 6
    @YellPika Yes they do. They waste cycles doing nothing. They are one of the things that make debug builds slower. – Tim Lloyd Jan 04 '11 at 03:30
  • 1
    The JIT compiler should optimize them out of the native instruction flow though. – devstuff Jan 05 '11 at 07:28
  • 1
    @devstuff I am talking about Debug builds - they are not optimized out. I would not have added it as a tip if that was the case. – Tim Lloyd Jan 05 '11 at 07:30