afaolek
afaolek

Reputation: 8831

Should there be any need to type 'static' before each member in a static class?

Since a static class can contain only static members, should there be any need to type static before each member?

What I really want to know is that: can a static class contain anything other than static members? If not, the compiler should help to append static to all members of a static class rather than complain that ...

Upvotes: 2

Views: 80

Answers (1)

dcastro
dcastro

Reputation: 68720

Since a static class can contain only static members, should there be any need to type static before each member?

There is no need, per se, but it makes your code much more readable.

The same argument could be made for interface implementations - since they have to be public, why do we need to explicitly mark them as public?

Imagine a world where you don't have to mark interface implementations as public:

public interface I
{
    void M();
}

public class C : I
{
    void M();

    void M2();
}

Methods M and M2 seem to have the same level of access. However, M is public, and M2 is private. By having the compiler force you to mark M as public, the issue goes away.

Readability and consistency are the reasons why you're forced to mark interface implementations as public and members of a static class as static: public members are always marked as public and static members are always marked as static.

Upvotes: 5

Related Questions