Why C# allow this fun? ; ; ; ; [duplicate]

2020-08-09 07:31发布

see my below code

protected void Page_Load(object sender, EventArgs e)
{
    ExistsOnPath("efsui.exe"); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
    install(); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
}

I have add more semicolon symbol in visual studio. Why visual studio allowed this? Is it wrong or correct?

See this image and red mark. build is succeeded

enter image description here

But error shows in this line ?

webClient.DownloadFile("http://www.who.int/inf-new/dnldtxt/introductions.zip", "";;;;;;;;;;;;;;;;);

enter image description here

Another fun, the below line is succeeded

private void install()
    {
        http://www.stackoverflow.com
        return;
    }

标签: c# syntax
3条回答
我只想做你的唯一
2楼-- · 2020-08-09 07:47

It succeeds because each statement that ends with a semi-colon doesn't have any syntax errors; they are empty. Now, as far as the generated code, nothing would get generated for those statements.

To better understand it you'd probably want to research how parsers work.

查看更多
贪生不怕死
3楼-- · 2020-08-09 07:54

The ; can be used as an empty statement in the C# language. Hence having many ; in a row is just fine because it's just a series of empty statements. Basically a no-op

Note that this is not Visual Studio behavior but instead C# language behavior. The same code in other languages like VB.Net would produce a compilation error

查看更多
Explosion°爆炸
4楼-- · 2020-08-09 07:59

Because semicolon ; is a valid Empty Statement

8.3 The empty statement

An empty-statement does nothing.

empty-statement:

;

An empty statement is used when there are no operations to perform in a context where a statement is required.

Execution of an empty statement simply transfers control to the end point of the statement. Thus, the end point of an empty statement is reachable if the empty statement is reachable.

Also see: Statements (C# Programming Guide) - MSDN

The empty statement consists of a single semicolon. It does nothing and can be used in places where a statement is required but no action needs to be performed.

EDIT:

webClient.DownloadFile("http://www.who.int/inf-new/dnldtxt/introductions.zip", ;;;;;;;;;;;;;;;;"");

But Why error shows in this line ?

well ; semicolon is a valid empty statement but that doesn't mean that you can place that anywhere in your code. DownloadFile method expects two parameters , would you place any statement in it like:

webClient.DownloadFile("http://www.who.int/inf-new/dnldtxt/introductions.zip",
                                             Console.Write("Some Text");,  "");

Where Console.Write is a valid statement itself, but it can't be used for parameter.

Why this code compiles:

private void install()
{
    http://www.stackoverflow.com
    return;
}

Because it is treating http: as a Labeled statements and anything after the colon is considered as part of label text. The above code will produce a warning

This label has not been referenced

查看更多
登录 后发表回答