Static Classes

static class is a class that contains only static members and cannot be instantiated.  You declare a class as static using the static keyword.
A static class:
  • Can only have static members (can’t contain instance members)
  • Cannot be instantiated
  • Cannot serve as the type of a variable
  • Cannot serve as a parameter type
  • Cannot inherit from another class
  • Cannot serve as a parent of another class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static class DogMethods
{
    private static int NumBarks = 5;
 
    public static void BarkALot(Dog d)
    {
        for (int i = 1; i <= NumBarks; i++)
            d.Bark();
    }
 
    public static string MarriedName(Dog d1, Dog d2)
    {
        return d1.Name + d2.Name;
    }
}
1
2
3
4
5
Dog d1 = new Dog("Kirby", 12);
Dog d2 = new Dog("Lassie", 47);
 
DogMethods.BarkALot(d1);
Console.WriteLine(DogMethods.MarriedName(d1, d2));