Why do members of a static class need to be declared as static? Why isn't it just implicit?

ぃ、小莉子 提交于 2019-11-26 07:41:24

问题


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?


回答1:


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.




回答2:


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




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/6005109/why-do-members-of-a-static-class-need-to-be-declared-as-static-why-isnt-it-jus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!