为什么 C# 静态构造函数需要括号?
考虑一下:
class Foo
{
static Foo()
{
// Static initialisation
}
}
为什么 static Foo() {...}
中需要 ()
?静态构造函数必须始终是无参数的,那为什么还要麻烦呢?它们是否有必要避免一些解析器的歧义,或者只是为了保持与常规无参数构造函数的一致性?
由于它看起来非常像初始化块,因此我经常发现自己不小心将它们遗漏了,然后不得不花几秒钟思考出了什么问题。如果它们能以同样的方式被省略就好了。
Consider:
class Foo
{
static Foo()
{
// Static initialisation
}
}
Why are the ()
required in static Foo() {...}
? The static constructor must always be parameterless, so why bother? Are they necessary to avoid some parser ambiguity, or is it just to maintain consistency with regular parameterless constructors?
Since it looks so much like an initialiser block, I often find myself leaving them out by accident and then have to think for a few seconds about what is wrong. It would be nice if they could be elided in the same way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这是为了消除歧义:它使解析器的工作更容易,将代码块识别为构造函数子例程(无论
静态
性);相反,它有助于确保人类作者/维护者了解选择此特定构造的含义,通过强制他们使用特定的类似方法的语法。I would assume it's for disambiguity: it makes the parser's job easier, recognising the code block as a constructor subroutine (irrespective of
static
ness); and conversely it helps ensure that the human author/maintainer is aware of the implications of choosing this particular construct, by forcing them to use a specific method-like syntax.我经常收到这样的问题;也就是说,问题“编译器可能会发现这个东西丢失了,那么为什么需要它?”这是此类问题的另一个示例:
C# 在静态类中使用常量< /a>
正如我在该问题中指出的,基本上在这种情况下我们有三种选择。将多余的文本设置为必需、可选或非法。
每个都有其自己的缺点。
使其成为必需的缺点是最终会在语言中产生不必要的冗余。
使其成为可选的缺点是您会让那些认为这两种形式之间一定存在差异的人感到困惑。此外,您还使错误恢复解析器更难完成其工作;它靠冗余而繁荣。而且您可能会在未来添加新的语言功能变得更加困难,因为已经声明了更多的“语法区域”。
使其非法的缺点是您会遇到“陷阱”,用户必须记住哦,是的,我应该在此处放置括号,但不是在这里。
所提议的功能最好有一个可以弥补缺点的优点。在我看来,最小的缺点是第一个:让它成为必需的。我希望其他选择能够有一个优点来证明其缺点是合理的,但我在这里没有看到一个。
I get this sort of question frequently; that is, the question "the compiler could work out that this thing is missing, so why is it required?" Here's another example of this sort of question:
C# using consts in static classes
As I noted in that question, basically we have three choices in that situation. Make the redundant text required, make it optional, or make it illegal.
Each has its own downside.
The downside of making it required is you end up with an unnecessary redundancy in the language.
The downside of making it optional is you confuse people who think there must be a difference between the two forms. Also, you make it harder for the error-recovering parser to do its work; it thrives on redundancy. And you potentially make it harder to add new language features in the future, because more "syntactic area" is already claimed.
The downside of making it illegal is you then make a "gotcha", where the user has to remember that oh, yeah, I'm supposed to put parens here, but not here.
The proposed feature had better have an upside that pays for the downside. The smallest downside seems to me to be the first: make it required. The other options I would want to have an upside that justifies the downside, and I'm not seeing one here.
因为它是一个静态构造函数,所以它是
静态
+ 一个普通的构造函数。一致性是关键。 :-)
Because it's a static constructor, so it's
static
+ a normal-looking constructor.Consistency is key. :-)