#517 – Static Classes
February 10, 2012 2 Comments
A 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
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; } }
Dog d1 = new Dog("Kirby", 12); Dog d2 = new Dog("Lassie", 47); DogMethods.BarkALot(d1); Console.WriteLine(DogMethods.MarriedName(d1, d2));