为什么有些线路没有在C#中分号? [英] why do some lines not have semicolon in C#?

查看:167
本文介绍了为什么有些线路没有在C#中分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚的技术原因,在下面的一些行不以分号结束,但其他行做的 - 什么是关于一个分号C#中的某些行预计,那么别人....



在事实上,而写这我注意到,那必须有大括号{}不需要分号是对自己的Console.WriteLine的表述,但线条的确需要。它



真的想找到这个技术上的原因...



如:

 命名空间checkPackage ** //别无分号** 
{
类节目** //别无分号**
{
静态无效的主要(字串[] args)** //别无分号**
{
listFilesInDirectory(@C:\Temp\); ** //需要一个分号**
}

静态无效listFilesInDirectory(字符串工作目录)** //别无分号**
{
的String []文件路径= Directory.GetFiles(工作目录); ** //需要一个分号**

的foreach(在文件路径字符串文件路径)** //别无分号**
{
Console.WriteLine(文件路径); ** //需要一个分号**
}

}
}
}


解决方案

分号不是的的终结者......这是一个的语句终止。有一个分号在每一个表达式语句和声明语句的结束。



如果等报表不表达或声明语句)



因此,例如:

 公共类Foo //部分类声明
的{
INT X = 0; //声明语句

公共无效酒吧()//方法声明
部分{
Console.WriteLine(x)的; //表达式语句(使用调用表达式)
} //方法声明结束了,但没有一个声明语句

} //类的声明结束了,但没有一个声明语句

要求他们的目的是为了让编译器可以告诉当你想结束的声明,而不是继续下一行:

  INT X = 5; 
INT Y = X //这不是语句的结束!
+ 5; //这是...



一种选择(使用VB,例如)被有行要明确继续到尽管当前线将是一个有效的语句的下一行继续。



正如评论指出的那样,语句是这里异常。我看不出有什么明显的原因,这不应该是有效的:

 做{}而(假)

...但事实并非如此。它的可能的有关该平原,而语句需要声明的身体,例如事实,而(真); (空语句)或,而(真){} (块语句)。我能来最接近的是因为C#规范说的不要语句结尾需要一个分号......


I am just trying to figure out the technical reason why in the below some lines do not end with a semicolon but other lines do - what is it about a semicolon that C# expects in some lines then others....

In fact whilst writing this I noticed that the statements that have to have curly brackets {} do not need semicolons but the lines that are on its own "Console.WriteLine" do need it.

Really trying to find the technical reasons for this...

ie:

namespace checkPackage     **//no semicolon**
{
    class Program      **//no semicolon**
    {
        static void Main(string[] args)     **//no semicolon**
        {
            listFilesInDirectory(@"C:\Temp\");    **//NEEDS a semicolon**
        }

        static void listFilesInDirectory(string workingDirectory)   **//no semicolon**
        {
            string[] filePaths = Directory.GetFiles(workingDirectory);  **//NEEDS a semicolon**

            foreach (string filePath in filePaths)   **//no semicolon**
            {
                Console.WriteLine(filePath);  **//NEEDS a semicolon**
            }

        }
    }
}

解决方案

The semi-colon isn't a line terminator... it's a statement terminator. There's a semi-colon at the end of every expression statement and declaration statement.

(if, for etc statements aren't expression or declaration statements.)

So for example:

public class Foo // Part of a class declaration
{
    int x = 0; // Declaration statement

    public void Bar() // Part of a method declaration
    {
        Console.WriteLine(x); // Expression statement (using an invocation expression)
    } // End of the method declaration, but not a declaration statement 

} // End of class declaration, but not a declaration statement

The purpose of requiring them is so that the compiler can tell when you wanted to end the statement instead of continuing on the next line:

 int x = 5;
 int y = x // This isn't the end of the statement!
         + 5; // This is...

One alternative (used by VB, for example) is to have a line continuation where you want to explicitly continue onto the next line even though the current line would be a valid statement.

As noted in comments, the do statement is an anomaly here. I see no obvious reason why this shouldn't be valid:

do { } while (false)

... but it isn't. It may be related to the fact that the plain while statement needs a statement body, e.g. while (true); (empty statement) or while (true) {} (block statement). The closest I can come is "because the C# specification says the do statement needs a semi-colon at the end..."

这篇关于为什么有些线路没有在C#中分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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