Simple C# Noop Statement

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.


Solution 1:

If you really want noop, then this defines a nameless action that doesn't do anything, and then invokes it, causing nothing to happen:

((Action)(() => { }))();

Solution 2:

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

Solution 3:

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.