开发者

Why mustn't I put a semicolon at the end of class declaration in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

In a C# class declaration:

class thing
{
    ...
}

Why, at the end of class declaration, must I not include the semi开发者_Go百科colon? It's really different from C++. You can see Why must I put a semicolon at the end of a class declaration in C++?


Because that is what the language specification says.

Remember that C# wasn't designed with any C++ compatibility in mind. The language designers has simply decided, that the ending semi-colon is not needed.


Because the language specification (page 263) says it is optional.


If you use semicolon after block declarations, your application will still compile fine but you'll get a warning that you have an extra semicolon. C# does not expect blocks to be terminated with semicolon by default.

public class MyClass
{
    public void MyMethod()
    {
        // Arbitrary block
        {
        }; // Semicolon here is fine but not required
    }
}; // Semicolon here is also fine but not required


You CAN put the semicolon at the end of a class declaration if you want, but it's not required. It's optional by design, probably to maintain style compatibility with C++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜