Translate

Tuesday, September 4, 2012

What is static class and why we use it?

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.

Advantage of static class

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.
Static class are used when a class provides functionality that is not specific to any unique instance. It provide the functionality a global access. 
Static methods do not consume any dynamic memory. Static class is load in stack memory and so the execution will be fast.

No comments:

Post a Comment