2

I'm trying to understand the c# language and why you require the static keyword on all members within the static class. Yes, I understand a static class cannot be instantiated, but why aren't members by default static within a static class, as we know that a static class cannot have non-static members?

For Example: Why can't this

public static class StaticClass
{
    public static int numberTest = 2;
}

be:

public static class StaticClass2
{
    public int numberTest = 2;
}
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
DJJ
  • 203
  • 1
  • 2
  • 9
  • The word static says it all. Static methods doesn't need an instance and that method needs to access the variable without the class instance. – Rohit Prakash Feb 11 '15 at 08:48
  • Ah yes, an answer from Eric Lippert (in the linked answer) is fairly authoritative. :) – Matthew Watson Feb 11 '15 at 09:05
  • Agreed, read through the other thread with similar question and understand it... Just weird that I could not find it myself before posting the question. – DJJ Feb 11 '15 at 09:08

2 Answers2

4

It is a design decission by the language designers. Of course a static class can have static members only, so the static qualifier is redundant. But it makes things clearer and less error-prone if you are forced to put it there. This becomes more and more important when your classes and projects get larger.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • Yes, I agree it is redundant, hence the question... I understand as a developer that it would be important in large projects, but I was just interested in knowing the reasons behind it. – DJJ Feb 11 '15 at 08:55
  • In an interface the `public` modifier is implied and it is disallowed to add it yourself. There seems to be a parallel there, with different choices being made. – H H Feb 11 '15 at 08:56
2

Most likely for historic reasons. You are right that an automatic, implied static would be more in line with other parts of the language.

But static classes were new in C# 2.0, and the change had to be non-breaking.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Also it seems better that adding the `static` modifier to a class declaration causes a compile error if some of the members are not static, rather than silently changing the behaviour of those members. – Matthew Watson Feb 11 '15 at 09:01
  • @MatthewWatson - it might have worked but the `static` modifier would have had to be disallowed on members (compare with `interface`). That would (kind of) break de-facto static classes written for C# 1. – H H Feb 11 '15 at 09:01
  • Agreed (I removed my earlier comment as you were replying, sorry!) – Matthew Watson Feb 11 '15 at 09:01
  • Agreed and makes complete sense, however, it would be a different scenario on new static classes without any members... But accept the response – DJJ Feb 11 '15 at 09:04
  • I found a dupe with an answer from Erric Lippert, he doesn't have to guess. – H H Feb 11 '15 at 09:05
  • I will accept this answer based on the comments, however, the actual answer can be updated as well. Thanks @HenkHolterman – DJJ Feb 11 '15 at 09:13