Search code examples
c#staticstatic-classes

C# Static Classes - A static class cannot have non-static members


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;
}

Solution

  • 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.