Simple C# Noop Statement

后端 未结 15 733
面向向阳花
面向向阳花 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:51

    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 ''::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#.

提交回复
热议问题