#784 – When Not to Use a Static Class

You can create a static class as a container for a collection of static utility methods that you can invoke without having to create an instance of some object.

There are a few drawbacks to using static classes, however:

  • Global data and concurrency problems – static data within a static class is effectively global data, which can lead to concurrency problems when multiple threads access/change the static data
  • Allure of monolothic classes – it’s easy to end up with large static classes containing a bunch of unrelated methods
  • Can’t extend behavior of a static class – because you can’t inherit from a static class, you can’t extend the class’ behavior by using inheritance
  • Harder to isolate, for testing purposes – since we can’t treat the static class as an interface, we can’t swap out the actual implementation for a mocked version of the class

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.

One Response to #784 – When Not to Use a Static Class

  1. Roe says:

    When to use a static class?

    ……Never IMO. If you can do it without a static then do it that way instead and your future self may thank you.

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: