Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 432
richard Avatar asked May 14 '11 22:05

richard


People also ask

Why can't we declare a class as static?

We can't declare outer (top level) class as static because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class.

What happens if a class is declared as static?

A class can be declared static only if it is a nested class. It does not require any reference of the outer class. The property of the static class is that it does not allows us to access the non-static members of the outer class.

Can we declare non-static members in static class?

Static class can't contain non-static members because by definition it can't be instantiated so there's no possibility to use these members.

Why do we need static class in C#?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.


2 Answers

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.

like image 147
Eric Lippert Avatar answered Oct 24 '22 07:10

Eric Lippert


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

like image 32
soandos Avatar answered Oct 24 '22 05:10

soandos