Does C# allow double semicolon ; ; if so, are there any special ways?

后端 未结 7 1702
盖世英雄少女心
盖世英雄少女心 2020-11-29 12:18

I am writing a statement and it compiles, but the compiler [VS] never tells me that I put the semicolon two times.

This means in ASP.NET MVC 3

return         


        
7条回答
  •  难免孤独
    2020-11-29 12:47

    That a double ;; is allowed, is for historical reasons. It's is a hangover from C style languages (which C# is based on).

    C & C++ have the concept of pre-processor macros which are replaced in the code before the code is compiled e.g. trivial example AddAndSquare is a macro, not a function

    #define AddAndSquare(X,Y) (X+Y)*(X+Y)
    int Foo() {
       int a = 1, b = 2;
       return AddAndSquare(a, b);
    }
    

    goes to the compiler as

    int Foo() {
       int a = 1, b = 2;
       return (A+B)*(A+B);
    }
    

    You can redefine macros to be different to their initial definition, also you can redefine them so they don't exist at all.

    Given an assertion macro #define ASSERT(c) if(!c) throw new AssertionFailedException() you can have your coded littered with ASSERT statements.

    void Foo(int x) {
        int y = x + 2;
        ASSERT(y != 0);
       int z = x / y;
        . . . .
    }
    

    Now consider that you only want the asserts in debug builds, but not in release builds, for release you redefine the macro to be empty (literally #define ASSERT). Now when Foo goes to the compiler for a release build, it looks like this

    void Foo(int x) {
        int y = x + 2;
        ;
       int z = x / y;
        . . . .
    }
    

    There's now an empty statement where the ASSERT was, because there may or may not be a statement there (depending on build configuration), the compiler needs to be able to handle an empty statement.

    Why this convention was kept in C# where there are nothing like C macros, I have no idea, but possibly because it causes little or no harm.

    I would guess that multiple ; are elided by the compiler before it starts parsing code, therefore your unreachable ; is ignored by the compiler.

提交回复
热议问题