为什么C#允许这样好玩吗? ; ; ; ; [英] Why C# allow this fun? ; ; ; ;

查看:318
本文介绍了为什么C#允许这样好玩吗? ; ; ; ;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看我下面的代码



 保护无效的Page_Load(对象发件人,EventArgs五)
{
ExistsOnPath(efsui.exe); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
安装(); ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
}



我在Visual Studio中添加更多的分号标志。为什么视觉工作室允许吗?它是错误的或正确的?



请参阅此形象和红色标记。打造的是成功了。





但在这行错误显示?

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



在这里输入的形象描述



另一个有趣的,下面的线被成功

 私人无效安装()
{
http://www.stackoverflow.com
的回报;
}


解决方案

由于分号; 是一个有效的空语句



8.3空语句



$ ; b $ b

这是空语句什么都不做。



空语句:





在没有任何操作在需要声明
A上下文中执行空语句被使用。



<空语句p>执行就是将控制到语句的结尾
点。因此,空语句的结束点是
之间如果空语句是可到达




另请参见:<一HREF =http://msdn.microsoft.com/en-us/library/ms173143.aspx相对=nofollow>语句(C#编程指南) - MSDN




空语句由一个分号。它不执行任何
和可以在要求的陈述,但没有动作
需要执行的地方使用。




编辑:

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




但为什么错误显示了在这一行?




以及; 分号的是一个有效的空语句但不意味着你可以将代码中的任何地方。 DownloadFile 方法需要两个参数,你会不会把它喜欢的语句:

  webClient.DownloadFile(http://www.who.int/inf-new/dnldtxt/introductions.zip,
Console.Write(一些文字);,);



其中, Console.Write 是一个有效的声明本身,但是它不能被用于参数。




为什么这段代码编译:




 私人无效安装()
{
http://www.stackoverflow.com
的回报;
}

由于它是治疗 HTTP:标记语句的和冒号后的任何被视为标签文本的一部分。上面的代码会产生警告




这个标签没有被引用



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

But error shows in this line ?

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

Another fun, the below line is succeeded

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

解决方案

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

这篇关于为什么C#允许这样好玩吗? ; ; ; ;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆