#783 – When to Create a Static Class
February 19, 2013 2 Comments
A static class is a class that contains only static members and cannot be instantiated. You don’t create an instance of the class, but rather just access its members directly.
public static class DogUtil { public static void DoSomething() { Console.WriteLine("I'm doing something"); } }
public static class DogUtil { public static void DoSomething() { Console.WriteLine("I'm doing something"); } }
You typically use a static class to hold a collection of methods that are utility methods and don’t need to act upon data that is persisted within a particular object.
As an example, the System.Math class in .NET contains a lot of math utility methods. Each method operates in a stateless way. That is, the method accepts one or more parameters, does some calculations, and returns a result.
Pingback: Dew Drop – February 19, 2013 (#1,501) | Alvin Ashcraft's Morning Dew
Pingback: #784 – When Note to Use a Static Class « 2,000 Things You Should Know About C#