Static Class versus Static members (C#)

Static classes are used when a class provides functionality that is not specific to any unique instance, means there will be no instance created, but its functions, static variables etc can be used using the class name. Here are the features of static classes in C# 2.0.

  • Static classes cannot be instantiated.
  • Static classes are sealed so they cannot be inherited.
  • Only static members are allowed.
  • Static classes can only have static constructor to initialize static members.

Advantages

Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.

Also compiler makes sure that no instance members are declared within a static class.

In case we make functions/instance variables static instead of making the class as static, then for each request, an instance of the class would be created, which would be unnecessary like if we just need to use the class for having some constant/static messages/variables etc which are not an object specific. 

Advertisements
This entry was posted in .Net 2.0. Bookmark the permalink.

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