语句不能存在于方法之外吗?
读一本书(VS 2010),它说.NET Csharp中的命令(语句)不能存在于方法之外。 我想知道 - 字段声明等,这些是命令,不是吗?它们存在于班级层面。有人可以详细说明一下吗?
Reading a book (VS 2010), it says that commands (statements) in .NET Csharp cannot exist outside of method.
I am wondering - field declaration etc, these are commands, are they not? And they exist at class level. Can somebody elaborate at this a bit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您的意思是:
计数和缓冲区是使用初始化表达式表达式的声明。但这段代码不包含语句。
If you mean:
The count and buffer are declarations using initializer expressions . But this code contains no statements.
字段初始化程序是用方法外部的代码编写的,但编译器将该代码放在构造函数内。
所以像这样的字段初始化器:
基本上是构造函数中的一个字段和一个初始化器:
A field initialiser is written with the code outside a method, but the compiler puts that code inside the constructor.
So a field initialiser like this:
is basiclally a field and an initialiser in the constructor:
C# 中没有“命令”这样的概念。
静态/实例变量声明不属于 C# 中的语句 - 它是一个字段声明(这是一种类成员声明),根据C# 规范。请参阅 C# 4 规范 例如。
现在,声明局部变量的语句是语句,如规范中的声明语句所定义(第8.5节)。但它们只供当地人使用。有关 C# 4 中语句产生式的完整列表,请参阅 B.2.5 节。
基本上,C# 规范定义了所涉及的术语 - 因此,虽然您可能会非正式想到“命令”等,但实际上, C# 规范的正确性是权威的来源。 (当然,除非它没有说明语言设计者的意思。这种情况非常罕见。)
There's no such concept as a "command" in C#.
And a static / instance variable declaration isn't categorized as a statement within C# - it's a field-declaration (which is a type of class-member-declaration) as per the C# spec. See section 10.5 of the C# 4 spec for example.
Now the statements which declare local variables are statements, as defined by declaration-statement in the spec (section 8.5). They're only used for locals though. See section B.2.5 for a complete list of statement productions within C# 4.
Basically, the C# spec defines the terminology involved - so while you might think informally of "commands" and the like, in a matter of correctness the C# spec is the source of authority. (Except for where it doesn't say what the language designers meant to say, of course. That's pretty rare.)
正如你所说,它们是声明,声明是真正完成某件事的声明。
As you said they're declarations, a statement is one which actually gets something done.
不,它们是声明。准确地说,是类成员声明。
对于那些存在于方法之外的东西来说是完全合法的。否则,您一开始就无法声明方法!
通过“语句”,本书告诉您不能在方法之外进行方法调用之类的事情。例如,下面的代码是非法的:
No, they're declarations. Class member declarations, to be precise.
And it's perfectly legal for those to exist outside of a method. Otherwise, you couldn't declare a method in the first place!
By "statements", the book is telling you that you can't have things like method calls outside of a method. For example, the following code is illegal:
类、命名空间、字段声明不是声明语句。
可以使用表达式在方法外部初始化字段,但是虽然表达式是语句,但有很多语句不是表达式(例如
if
)。这一切都取决于语言语法如何定义术语,而 C# 的定义方式非常常见(例如,与 C 和 C++ 非常相似)。
Classes, namespace, fields declarations are not declarations statements.
A field can be initialised outside a method with an expression but while an expression is a statement there are lots of statements that are not expressions (eg.
if
).It all comes down to how the language grammar defines the terms, and the way C# does it is pretty common (eg. very similar to C and C++).