What is a simple Noop statement in C#, that doesn't require implementing a method? (Inline/Lambda methods are OK, though.)
My current use case: I want to occupy the catch-block of a try-catch, so I can step into it while debugging and inspect the exception.
I'm aware I should probably be handling/logging the exception anyway, but that's not the point of this exercise.
You can write a function that does nothing.
If you really want noop, then this defines a nameless action that doesn't do anything, and then invokes it, causing nothing to happen:
But using that standard
;
as a branch of anif
statement makes MS Visual Studio 2010 show a Warning: "Possible mistaken empty statement". (Warning CS0642, though VS2010 doesn't tell me that or link to actual help for the warning.)Worse, the MSDN C# Language Specification does not mention that actually coding that empty statement as a branch of an
if
statement provokes Warning CS0642 "Possible mistaken empty statement". (Warning because it is "bad form", potentially ambiguous.)Worse yet, it looks like VS2010 provides no way to NEATLY suppress an individual warning. I would have to insert
#pragma warning disable CS0642
before the line(s) and [optionally]#pragma warning disable CS0642
after. To me, this is uglier than the warning. I'd be better off using{ }
in place of;
. (I might use an override that is a little less ugly.)I looked here for a "C# no-op" because I wanted an alternative to the "empty statement", to get rid of that warning. I don't need a checkpoint. I just want a do-[absolutely]-nothing that is not ambiguous like "the empty statement".
The alternative must not provoke SOME OTHER warning.
int u;
is no good because it provokes Warning "The variable 'u' is declared but never used".int u = 0;
is no good because it provokes Warning "The variable 'u' is assigned but its value is never used".If
noop;
(or similar) were added as an unambiguous empty statement (NOT a macro definition), that would be great.If
noop();
(or similar) were a function with an empty body (which can disappear completely when the compiler inlines it), that would almost be great.When the branch is only one statement, I often omit the surrounding
{
and}
LINES because they are not needed and they stretch the code vertically, making it harder to read. The inconsistency in the language is that I can't omit the the surrounding{
and}
LINES when they surround ZERO statements. I can compact the two lines to{ }
on the same line, but that is inconsistent. I think;
on a line is the neatest solution, and it should NOT cause a warning on the [unstated] grounds of "bad form". I think warning CS0642 should have defaulted to OFF. I think the following code should be acceptable as-is:(I lamented being unable to write this as a comment because I did not yet "have 50 reputation to comment". Now that I can comment, at 2K bytes, it is too long for a comment by 1.5K bytes, so it's staying here.)
Well the NOP in C# exists, as in C and is
';'
and its correct definition is "the empty statement", but for the usage you intend, is enought to put the breakpoint in the closing catch bracket... There is no needing to Keep Alive anithing, since Tthe lifetime of an object reference in a method is extended to the end of the method when the debugger is attached. So you simply need to writeand put the breakpoint on the closing bracket and see the exception content.
Are you trying to debug a release (optimised) build? It is normally the optimiser that removes unreferenced variables and empty blocks.
Two solutions:
catch
itself and use$exception
– created by the debugger to reference the exception in flight – in the Locals tool window.I know this is an old question and, technically, this answer doesn't relate to the asker's use case. However, there is a NOOP instruction in CIL, which is
nop
. As an experiment, take the following CIL application.If you compile the application, and decompile it with a tool like ILSpy, to C#, this is the contents of the main() method:
As you can see, there is nothing there. However, if we want to verify that the CIL compiler didn't optimize out these
nop
statements, we can view our application in decompiled IL code in ILSpy, and this is what we see for the main method:CIL is certainly compiling the
nop
instructions into the assembly. Since C# has no implementation of this instruction, thesenop
commands are not shown within the disassembled C# code.I don't have a license for Reflector but I imagine if you decompile these binaries with Reflector you would get similar output for C#.