Simple C# Noop Statement

2019-03-10 23:48发布

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.

标签: c# noop
15条回答
时光不老,我们不散
2楼-- · 2019-03-11 00:35

How about:

GC.KeepAlive(e);

where e is the exception variable?

(I haven't tried putting a break point on the catch declaration itself. It feels like you ought to be able to do that, precisely for this reason. But whether it works or not is a different matter.)

Or somewhat more cryptically, assuming you've already got a using directive for System.LINQ:

"".AsEnumerable();
查看更多
Bombasti
3楼-- · 2019-03-11 00:37

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-03-11 00:38

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.

查看更多
爷、活的狠高调
5楼-- · 2019-03-11 00:40

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

查看更多
三岁会撩人
6楼-- · 2019-03-11 00:42

You can just write:

catch {
    ;
}

The empty statement with a single semicolon is the C# NOOP.

查看更多
一纸荒年 Trace。
7楼-- · 2019-03-11 00:43

This is an addition to @AHM 's answer since I wanted an easy way to do NOOP for debugging purposes (communicating with AB PLC CompactLogix and ran into errors only really visible in Disassembly because of C++ library DLL import in C#).

I took the one-liner

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

and put it into a snippet named noop.snippet then placed it in the folder named My Code Snippets.
(Tools -> Code Snippets Manager -> Location) OR Chord (Ctrl+K,Ctrl+B)

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>noop</Title>
            <Shortcut>noop</Shortcut>
            <Description>Code snippet to inject an assembly (x86) equivalent of the NOOP command into the code's disassembly.</Description>
            <Author>Jay Whaley</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Code Language="csharp">
            <![CDATA[// Forces a psuedo NOOP in disassembly
                ((Action)(() => { }))();
            $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

This helps to make it a quick use shortcut in case low level communication becomes muddled and requires this to be a common debugging tactic. The actual assembly generated is as follows, but there're some posts about how to use actual assembly inline in C#.

Disassembly generated from invoking a nameless action

查看更多
登录 后发表回答