0

I know that a static class cannot have non-static methods. It makes sense. But if so, why isn't all methods in a class implicitly static?

public static class Foo
{
    public void DoStuff(); // Does not compile.
}

In the example above, why doesn't the compiler implicitly make the method DoStuff() static if that's the only option? I guess it could be a readability thing, but is there anything more to it?

For example, the compiler implicitly sets different access modifiers for different types (classes, enums, interfaces, nested etc...). Couldn't it do the same for static methods?

The reason why I ask is that I'm currently writing a lot of HtmlHelpers of a MVC-project where the helper class is static, and I'm constantly missing to mark the methods as static.

smoksnes
  • 10,509
  • 4
  • 49
  • 74
  • 1
    memory consumption and access level are two different things Boss :) – Chandan Kumar Jul 26 '16 at 09:52
  • 1
    I can only guess, but perhaps the language designers thought that since in most cases, specifiers on a type and specifiers on a type's members cannot be derived from each other unambiguously, the best course would be that they must always be specified explicitly. For example, an `abstract` type doesn't necessarily contain only `abstract` members. A `public` type doesn't imply only `public` members. A type with `virtual` members doesn't have to be declared `virtual` (cannot, in fact). Perhaps `static` types are the only case where all members must be `static`, too. – stakx - no longer contributing Jul 26 '16 at 09:57
  • @kumarch1 - Yes, these are very different things. It was only an example for when something is required and something's optional. But the question is in fact a duplicate and the accepted answer was great. – smoksnes Jul 26 '16 at 10:18

0 Answers0