Qdot543
Qdot543

Reputation: 51

C# Syntax static class, makes static members redundant?

Note: This is the first time asking a question about a general curiosity regarding syntax. Because of this I might not be using the correct terminology to describe my question, which may mean its already been answered, and I can't find it because I'm unaware of what terms to use in the search. If that is the case, please post comments so I can edit and refine the question to meet the standard expected by stack overflow. Thank you.

Now the question. I've been using the static keyword a lot recently because I have members I need to access from anywhere without an instance. However its becoming increasingly tedious to declare every member and method static, when the class its self is already static. Since if you declare a class static, it means you can't create an instance of that class (that's my current understanding).

public static class Foo {}

Why does every member have to also be declared static.

public static class Foo 
{
    public static int X;
    public static int Y;
}

I would have thought that since the class, foo in this case, is declared to be static, all its members would automatically be static, and you no longer need to declare each member as static.

Obviously you can't do that, you have to declare every subsequent member static. However this feels counter intuitive and redundant to me.

Whats the reason for this?

Upvotes: 0

Views: 227

Answers (3)

Zenilogix
Zenilogix

Reputation: 1403

For consistency - uniformity of meaning. Making a class static doesn't change anything about the class; all it does is forbid the declaration of instance members. The class itself is neither instance nor static. It would be more concise if static on a class implied all members static, but one could argue that doing so would introduce a variable interpretation for a member declaration where not declared static. As it is, members are instance unless the static modifier is used.

Upvotes: 1

Warren Rumak
Warren Rumak

Reputation: 3874

Put simply -- it's for readability. You can look solely at the property definition and know that it's static, without having to look at the class definition.

Your two-line example may make it look unnecessary, but if you have a class with 500, 1000 or more lines of code, you will appreciate the extra clarity when digging through lots of methods, properties and fields.

That's the thing about software development: Never ever worry about a bit of extra typing -- you will read your code many more times than you will write it.

Upvotes: 4

Brian Mains
Brian Mains

Reputation: 50728

From what I understand, static on the class is a way to enforce all members are static (remove static from one, and it should give you an error). But yes, there is the requirement of defining the methods static. Technically, you wouldn't need to define static on the class.

You could, as an alternative, define a class, instantiate the class, and store the class reference globally, thus not needing the static definition on each member. The singleton pattern acts very similar in nature to static classes, except you define one common instance shared throughout.

Upvotes: 2

Related Questions