这段 C# 代码有什么问题?
public bool IsValid()
{
get { return (GetRuleViolations().Count() == 0); }
}
我收到此错误:
;预计
有什么问题吗?
我正在关注本教程: http://nerddinnerbook.s3.amazonaws.com/Part3.htm 我不确定他们为什么使用 get。
public bool IsValid()
{
get { return (GetRuleViolations().Count() == 0); }
}
I'm getting this error:
; expected
What is wrong?
I'm following this tutorial: http://nerddinnerbook.s3.amazonaws.com/Part3.htm
I'm not sure why they are using get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IsValid 之后不需要括号,因为它是一个属性而不是方法(大概是因为您使用的是 getter)
像这样编写:
或者,如果 IsValid 是 一个方法,它可能看起来像这样:
You don't need parentheses after IsValid, since it's a property not a method (presumably, since you're using a getter)
Write it like this:
Alternatively, if IsValid were a method, it could look like this:
这是一个有趣的例子,错误报告启发法出错了。这里发生的事情是编译器看到
并对自己说“啊哈,这里有一个名为 IsValid 的公共方法,它不带参数并返回 bool。我将在方法主体的块中将其后面的所有内容作为语句列表进行处理。
然后它看到
一个有趣的事实:get 不是 C# 的保留字(我讨论了这个事实 这里。)
所以此时编译器认为这是一个以标识符“get”开头的语句。该语句可能是什么?可能是局部变量声明:
它可以是方法或委托调用:
它可以是名为 get 的字段的增量或减量
它可以是标记语句的标签
方法调用的接收者。
它可以是 对字段的赋值:
它可能是一个事件加法器:
我确信我遗漏了十几种情况,我的观点是它可能是其中的任何一个,编译器将查看下一个要尝试的标记。并找出我们实际上处于这几十种情况中的哪一种。然后编译器实际看到的是
并且它的原因是“嗯,这很糟糕。这是那里一个新区块的开始。我知道在标识符获取之后和新块开始之前必须有一些东西,但我不知道它是什么。它可以是冒号、点、加号……我不知道,它几乎可以是任何东西。有什么我知道的吗?是的。我知道至少,以 get 开头的语句末尾和后面的块的开头之间总是必须有一个分号。因此,我将报告“缺少分号”错误,因为这是我目前能做的最好的事情。”
我们可以做的是特殊情况“get 是方法块中的第一个东西,它可以是一个属性,并且是紧接着是左大括号”,然后在这种情况下报告一个特殊错误“嘿,看起来你正在尝试编写一个带有参数的属性”。但显然我们在设计错误启发法时没有考虑到这种情况这是一个很好的做法,所以也许我们会在假设的未来版本的编译器中这样做。
This is an interesting case where the error reporting heuristics get it wrong. What's happening here is the compiler sees
And says to itself "aha, here we have a public method called IsValid that takes no arguments and returns bool. I will process everything after it as a list of statements in the block of the method body.
Then it sees
An interesting fact: get is not a reserved word of C#. (I discuss this fact here.)
So the compiler at this point now thinks that this is a statement beginning with the identifier "get". What could that statement possibly be? It could be a local variable declaration:
It could be a method or delegate call:
It could be an increment or decrement of a field named get.
It could be a label of a labelled statement.
It could be the receiver of a method call:
It could be an assignment to a field:
It could be an event adder:
And I'm sure there are a dozen cases I'm missing. My point is that it could be any of those things. The compiler will look at the next token to try and figure out which of those dozens of cases we're actually in. And then what the compiler actually sees is
and it reasons "Hmm, that's bad. That is the start of a new block there. I know that there had to be something after the identifier get and before the start of the new block, but I don't know what it was. It could be a colon, a dot, a plus... I don't know, it could be almost anything. Is there anything I do know? Yes. I know that at the very least, there always has to be a semicolon between the end of the statement that begins with get, and the beginning of the block which follows it. Therefore I will report "missing semicolon" error because that's the best I can do at this point."
What we could have done is special-cased the situation "the get is the first thing in a method block that could be a property and is immediately followed by a left curly brace" and then report a special error in that case "hey, it looks like you're trying to write a property that takes arguments". But apparently we did not think of this situation when designing the error heuristics. That's a good one, so perhaps we'll do that in a hypothetical future version of the compiler.
应该写成 :
第一行不带
()
。您会注意到您引用的网页上的内容是正确的。That should be written as :
without the
()
on the first line. You'll note that it is correct on the web page you cite.IsValid
必须是属性或方法。如果您希望它是一个方法,请将
()
保留在IsValid
之后,并放弃get
。如果您希望它成为属性,请删除
()
。IsValid
must be a property or a method.If you want it to be a method, leave the
()
afterIsValid
and ditch theget
.If you want it to be a property, remove the
()
.