Static Classes in C#

Posted: August 10, 2008 in .NET, CLR
Tags:

Normally in application development projects of any type we come across some sort of Utility classes, which has a bunch static methods for Date ,String .. etc manipulations.While reviewing code for the past few years I have seen two problems

  1. Some developers using object instances to access this methods without looking into the fact that they are static methods.
  2. During initial design & coding there are only static methods but down the line during support instance methods are getting added.

So we need to have a way where we can stop the instantiation of the class and addition of instance methods to it.

One option to do it in .NET 1.1 was making the class sealed with a private constructor.But that does not stop anyone from adding instance methods(of no use though) and private constructor takes up unnecessary metadata space.

In .NET 2.0 we have static classes for this purpose which can have only static members and can never be instantiated.

Advertisements

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