Static Classes

suggest change

The “static” keyword when referring to a class has three effects:

  1. You cannot create an instance of a static class (this even removes the default constructor)
  2. All properties and methods in the class must be static as well.
  3. A static class is a sealed class, meaning it cannot be inherited.
public static class Foo
{
    // There is no constructor because this cannot be an instance.
    public static int Counter { get; set; }

    public static int GetCount()
    {
        return Counter;
    }
}

        Foo.Counter++;
        Console.WriteLine(Foo.GetCount()); // Prints 1.

        // var foo1 = new Foo(); // Does not compile: Foo is static.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents