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?

1 Answer 1

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.
};
3
  • 6
    @YellPika Yes they do. They waste cycles doing nothing. They are one of the things that make debug builds slower.
    – Tim Lloyd
    Commented Jan 4, 2011 at 3:30
  • 1
    The JIT compiler should optimize them out of the native instruction flow though.
    – devstuff
    Commented Jan 5, 2011 at 7: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
    Commented Jan 5, 2011 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.