这段 C# 代码有什么问题?

发布于 2024-09-11 10:11:51 字数 352 浏览 3 评论 0原文

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 技术交流群。

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

发布评论

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

评论(4

甩你一脸翔 2024-09-18 10:11:51

IsValid 之后不需要括号,因为它是一个属性而不是方法(大概是因为您使用的是 getter)

像这样编写:

public bool IsValid
{
    get { return (GetRuleViolations().Count() == 0); }
}

或者,如果 IsValid 一个方法,它可能看起来像这样:

public bool IsValid()
{
    return (GetRuleViolations().Count() == 0);
}

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:

public bool IsValid
{
    get { return (GetRuleViolations().Count() == 0); }
}

Alternatively, if IsValid were a method, it could look like this:

public bool IsValid()
{
    return (GetRuleViolations().Count() == 0);
}
在梵高的星空下 2024-09-18 10:11:51

这是一个有趣的例子,错误报告启发法出错了。这里发生的事情是编译器看到

public bool IsValid() 
{ 

并对自己说“啊哈,这里有一个名为 IsValid 的公共方法,它不带参数并返回 bool。我将在方法主体的块中将其后面的所有内容作为语句列表进行处理。

然后它看到

get 

一个有趣的事实:get 不是 C# 的保留字(我讨论了这个事实 这里。)

所以此时编译器认为这是一个以标识符“get”开头的语句。该语句可能是什么?可能是局部变量声明:

get myGet = new get();

它可以是方法或委托调用:

get();

它可以是名为 get 的字段的增量或减量

get++;

它可以是标记语句的标签

get: M();

方法调用的接收者。

get.M();

它可以是 对字段的赋值:

get = null;

它可能是一个事件加法器:

get += M;

我确信我遗漏了十几种情况,我的观点是它可能是其中的任何一个,编译器将查看下一个要尝试的标记。并找出我们实际上处于这几十种情况中的哪一种。然后编译器实际看到的是

get {

并且它的原因是“嗯,这很糟糕。这是那里一个新区块的开始。我知道在标识符获取之后和新块开始之前必须有一些东西,但我不知道它是什么。它可以是冒号、点、加号……我不知道,它几乎可以是任何东西。有什么我知道的吗?是的。我知道至少,以 get 开头的语句末尾和后面的块的开头之间总是必须有一个分号。因此,我将报告“缺少分号”错误,因为这是我目前能做的最好的事情。”

我们可以做的是特殊情况“get 是方法块中的第一个东西,它可以是一个属性,并且是紧接着是左大括号”,然后在这种情况下报告一个特殊错误“嘿,看起来你正在尝试编写一个带有参数的属性”。但显然我们在设计错误启发法时没有考虑到这种情况这是一个很好的做法,所以也许我们会在假设的未来版本的编译器中这样做。

This is an interesting case where the error reporting heuristics get it wrong. What's happening here is the compiler sees

public bool IsValid() 
{ 

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

get 

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:

get myGet = new get();

It could be a method or delegate call:

get();

It could be an increment or decrement of a field named get.

get++;

It could be a label of a labelled statement.

get: M();

It could be the receiver of a method call:

get.M();

It could be an assignment to a field:

get = null;

It could be an event adder:

get += M;

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

get {

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.

甜尕妞 2024-09-18 10:11:51

应该写成 :

public bool IsValid
{ 
    get { return (GetRuleViolations().Count() == 0); } 
} 

第一行不带 () 。您会注意到您引用的网页上的内容是正确的。

That should be written as :

public bool IsValid
{ 
    get { return (GetRuleViolations().Count() == 0); } 
} 

without the () on the first line. You'll note that it is correct on the web page you cite.

倾城月光淡如水﹏ 2024-09-18 10:11:51

IsValid 必须是属性或方法。

如果您希望它是一个方法,请将 () 保留在 IsValid 之后,并放弃 get
如果您希望它成为属性,请删除 ()

IsValid must be a property or a method.

If you want it to be a method, leave the () after IsValid and ditch the get.
If you want it to be a property, remove the ().

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