语句不能存在于方法之外吗?

发布于 2024-11-07 11:33:01 字数 98 浏览 4 评论 0原文

读一本书(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

做个少女永远怀春 2024-11-14 11:33:01

如果您的意思是:

class Foo
{
    int count = 0;
    StringBuilder buffer = new StringBuilder();
}

计数和缓冲区是使用初始化表达式表达式的声明。但这段代码不包含语句

If you mean:

class Foo
{
    int count = 0;
    StringBuilder buffer = new StringBuilder();
}

The count and buffer are declarations using initializer expressions . But this code contains no statements.

小ぇ时光︴ 2024-11-14 11:33:01

字段初始化程序是用方法外部的代码编写的,但编译器将该代码放在构造函数内。

所以像这样的字段初始化器:

class Foo  {

  int Bar = 42;

}

基本上是构造函数中的一个字段和一个初始化器:

class Foo  {

  int Bar;

  Foo() {
    Bar = 42;
  }

}

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:

class Foo  {

  int Bar = 42;

}

is basiclally a field and an initialiser in the constructor:

class Foo  {

  int Bar;

  Foo() {
    Bar = 42;
  }

}
总以为 2024-11-14 11:33:01

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.)

恋竹姑娘 2024-11-14 11:33:01

正如你所说,它们是声明,声明是真正完成某件事的声明。

As you said they're declarations, a statement is one which actually gets something done.

拔了角的鹿 2024-11-14 11:33:01

不,它们是声明。准确地说,是类成员声明。

对于那些存在于方法之外的东西来说是完全合法的。否则,您一开始就无法声明方法!

通过“语句”,本书告诉您不能在方法之外进行方法调用之类的事情。例如,下面的代码是非法的:

public void DoSomething()
{
    // Do something here...
}

MessageBox.Show("This statement is not allowed because it is outside a method.");

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:

public void DoSomething()
{
    // Do something here...
}

MessageBox.Show("This statement is not allowed because it is outside a method.");
醉态萌生 2024-11-14 11:33:01

类、命名空间、字段声明不是声明语句。

可以使用表达式在方法外部初始化字段,但是虽然表达式是语句,但有很多语句不是表达式(例如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++).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文