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 c
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.The standard empty statement/noop operation in c# is
;
as in:
if (true)
;
(relevant documentation)
this specifically addresses your use case (just place a break-point on the ; line, or otherwise step to it), is minimal, and is directly supported by the environment just for this purpose (so you even if you're doing complex things, like looking at the compiled source, you won't have any additional noise/etc.. to worry about from the compiler/optimizer/etc...) - and has the additional benefit of putting up a warning, as a reminder to clean it out of your code when you're done debugging/push to production
If you want to break into the method you could hardcode a breakpoint:
System.Diagnostics.Debugger.Break();
Alternatively if you don't compile in release mode, the following line will emit IL which you can break on:
var a = 1;
You could also write a Debug.Break() that is specific to your machine:
[Conditional("DEBUG")]
[Obsolete("Please remove me before checkin.")]
public static void Break()
{
#IF DEBUG
if (Dns.GetHostName() == "PROTECTORONE")
Debugger.Break();
#ENDIF
}
Note that because of [Conditional("DEBUG")]
that method will not get called in call sites during a RELEASE build.
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.
.assembly extern mscorlib {}
.assembly Test
{
.ver 1:0:1:0
}
.module test.exe
.method static void main() cil managed
{
.maxstack 1
.entrypoint
nop
nop
nop
nop
ret
}
If you compile the application, and decompile it with a tool like ILSpy, to C#, this is the contents of the main() method:
static void main()
{
}
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:
.method static privatescope
void main$PST06000001 () cil managed
{
// Method begins at RVA 0x2050
// Code size 5 (0x5)
.maxstack 1
.entrypoint
IL_0000: nop
IL_0001: nop
IL_0002: nop
IL_0003: nop
IL_0004: ret
} // end of method '<Module>'::main
CIL is certainly compiling the nop
instructions into the assembly. Since C# has no implementation of this instruction, these nop
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#.
Reliable solution
try
{
blablablablaStatemnt();
}
catch(Exception ex)
{
#IF DEBUG
Debugger.Break();
#END IF
}
As simple as this!
Otherwise
can be very usefull;
In addition to the answers that directly answer the question.
If you just want to break, then you could always put the breakpoint on the opening {
or closing }
of the catch
block.