Simple C# Noop Statement

后端 未结 15 725
面向向阳花
面向向阳花 2021-02-01 01:14

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

相关标签:
15条回答
  • 2021-02-01 01:39

    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 write

    catch(Exception exception)
    {
    }
    

    and put the breakpoint on the closing bracket and see the exception content.

    0 讨论(0)
  • 2021-02-01 01:39

    I quite like this, just because it will confuse whoever comes across it:

    catch (SomeException e)
    {
        lock(e);
    } 
    
    0 讨论(0)
  • 2021-02-01 01:40

    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();
    
    0 讨论(0)
  • 2021-02-01 01:40

    Why over-engineer this?

    var x = 0;
    

    works just fine :)

    0 讨论(0)
  • 2021-02-01 01:43

    The standard empty statement/noop operation in C# is ; as in if (true) ;.
    - blueberryfields

    But using that standard ; as a branch of an if 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:

    if (condition1)
      action1;
    else if (condition2)
      ;  // (do nothing)
    else if (condition3)
      action3;
    else if (condition4)
      ;  // (do nothing)
    else if (condition5)
      action5;
    else
      action99;
    

    (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.)

    0 讨论(0)
  • 2021-02-01 01:45

    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)(() => { }))();
    
    0 讨论(0)
提交回复
热议问题