Why mustn't I put a semicolon at the end of class declaration in C# [closed]
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++.
精彩评论