#783 – When to Create a Static Class

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

2 Responses to #783 – When to Create a Static Class

  1. Pingback: Dew Drop – February 19, 2013 (#1,501) | Alvin Ashcraft's Morning Dew

  2. Pingback: #784 – When Note to Use a Static Class « 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: