为什么静态类的成员需要声明为静态?为什么它不只是隐式的?

发布于 2024-11-07 10:47:15 字数 50 浏览 4 评论 0原文

显然,静态类上不能有实例成员,因为该类永远无法实例化。为什么我们需要将成员声明为静态?

Obviously there can't be an instance member on a static class, since that class could never be instantiated. Why do we need to declare members as static?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

猛虎独行 2024-11-14 10:47:15

我总是被问到这样的问题。基本上,问题归结为“当编译器可以推断出有关已声明成员的事实时,该事实的显式声明应该是(1)必需的,(2)可选的,还是(3)禁止的?”

没有一个简单的答案。每一项都必须根据具体情况具体对待。需要将“static”放在静态类的成员上。将“new”放在派生类的隐藏、非重写方法上是可选的。禁止将“static”放在 const 上。

简要考虑一下你的情况,禁止它似乎很奇怪。您有一整个类,其中充满了标记为“静态”的方法。您决定将类设为静态,这意味着您必须删除所有静态修饰符?这很奇怪。

将其设为可选似乎很奇怪;假设您有一个静态类和两个方法,一种标记为静态,一种不标记为静态。由于静态通常不是默认值,因此很自然地认为它们之间存在差异。使其成为可选似乎可能会造成混乱。

这就使得它成为必需的,作为三个选项中最不坏的一个。

请参阅 http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx 有关这些的更多想法各种各样的问题。

I get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?"

There's no one easy answer. Each one has to be taken on a case-by-case basis. Putting "static" on a member of a static class is required. Putting "new" on a hiding, non-overriding method of a derived class is optional. Putting "static" on a const is forbidden.

Briefly considering your scenario, it seems bizarre to make it forbidden. You have a whole class full of methods marked "static". You decide to make the class static and that means you have to remove all the static modifiers? That's weird.

It seems bizarre to make it optional; suppose you have a static class and two methods, one marked static, one not. Since static is not normally the default, it seems natural to think that there is intended to be a difference between them. Making it optional seems to be potentially confusing.

That leaves making it required, as the least bad of the three options.

See http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx for more thoughts on these sorts of problems.

弥繁 2024-11-14 10:47:15

因为根据定义,它们的所有成员都必须是静态的。他们决定不提供一些令人困惑的语法糖。

Because by definition, all of their members must be static. They decided not to give some confusing syntactic sugar.

宁愿没拥抱 2024-11-14 10:47:15

我会更进一步问:为什么 C# 有静态类?这似乎是一个奇怪的概念,一个 类这并不是一个真正的类。它只是一个容器,您不能用它来输入任何变量、参数或字段。您也不能将其用作类型参数。当然,你永远不可能拥有这样一个类的实例。

我宁愿有模块,例如 VB.NETF#。然后,就不需要 static 修饰符来避免混淆。

I would go one step further and ask: Why does C# have static classes at all? It seems like a weird concept, a class that's not really a class. It's just a container, you can't use it to type any variables, parameters or fields. You also can't use it as a type parameter. And of course, you can never have an instance of such a class.

I'd rather have modules, like in VB.NET and F#. And then, the static modifier would not be necessary to avoid confusion.

一绘本一梦想 2024-11-14 10:47:15

它可能是隐式的,但也会使代码阅读变得复杂并导致混乱。

It could be implicit, but also it would complicate code reading and lead to confusions.

[旋木] 2024-11-14 10:47:15

理查德,

嗯...我猜测语言设计者认为最好非常非常明确...以避免维护者()维护时可能出现的任何混乱不知道代码,跳转到静态类的中间,并假设它们位于“正常”实例上下文中。

但当然,这只是一个猜测。无论如何,大多数 IDE 都会通过“自动”添加静态修饰符来帮助您……或者至少在“写入时”(而不是“编译时”)突出显示您的错误。

这是一个很好的问题...不幸的是没有一个“正确”答案...除非有人可以找到讨论这一决定的 C# 语言设计者博客(或类似博客)的链接。我可以告诉你的是:“我敢打赌 1,000 美元,这绝非偶然。”

干杯。基思.

Richard,

Hmmmm... I'd guess that the language designers decided that it would be better to be very, very explicit... to avert any possible confusion when a maintainer, who doesn't know the code, jumps into the middle of a static class, and presumes that they are in a "normal" instance context.

But of course, that's just a guess. Most IDE's help you out there anyway, by adding the static modifier "automagically"... or at least highlighting your mistake at "write time", as apposed to "compile time".

It's a good question... unfortunately not one with a "correct" answer... unless someone can turn up a link from a C#-language-designers blog (or similar) discussing this decision. What I can tell you is: "I'd bet $1,000 that it's no accident."

Cheers. Keith.

醉生梦死 2024-11-14 10:47:15

显式编码使事情可维护

如果我想将一个方法从一个类复制到另一个类,以便更好地组织代码,那么我必须一直检查很多事情,以防万一目标类是或不是静态的。

通过将成员声明为静态,当您看到代码时,您还可以直观地了解代码是什么。

它也不太混乱。想象一个静态类,其中的成员标记为静态,而其他成员则未标记。

我可以看到很多原因,而且还存在许多其他原因。

Explicit coding makes things maintainable

If I want to copy a method from one class to another, so that code is better organized, then I would have to keep cheking a lot of things all the time, just in case the destination class is or is not static.

By declaring the member as static, you also have a visual indication of what the code is, when you see it.

It is also less confusing. Imagine a class that is static, and inside it has got members marked as static, and others not marked.

I can see lots of reasons, and many other reasons exist.

一刻暧昧 2024-11-14 10:47:15

我认为显式声明它是静态变量很重要的一个原因是,在多线程编程模型中,这些静态变量由多个线程共享。在进行代码审查或代码分析时,通过读取变量(而不是查找类声明)来了解这种重要性并确定变量是静态还是非静态要容易得多。如果您不知道该类是静态的还是非静态的,那么在代码审查期间读取变量时可能会变得非常混乱。

One reason I would think it is important to explicitly state it is a static is because in a multi-threaded programming model, these static variables are shared by multiple threads. When doing code review or code analysis, it is much easier to pick up this importance from reading the variable, instead of looking up the class declaration, and determine if the variables are static or non-static. It can get pretty confusing when reading variable during code review if you don't know if the class is static or non-static.

记忆之渊 2024-11-14 10:47:15

这是因为复制粘贴会更复杂。

如果将方法从静态类复制到非静态类,则必须添加 static 关键字。

如果将方法从非静态类复制到静态类,则必须删除 static 关键字。

移动方法是开发人员要做的主要事情(“我需要重构这段代码,至少需要一周的时间”),通过让事情变得更容易,Eric 和他的团队让我们节省了工作时间。

This is because copy-paste would be more complicated.

If you copy a method from a static class to a non-static class then you would have to add the static keyword.

If you copy a method from a non-static class to a static class you would have to remove the static keyword.

Moving methods around is the primary thing developers do ('I need to refactor this code, it will take a week at least'), and by making it easier Eric and his team allowed us to save hours of work.

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