Tuesday, October 03, 2006

Static Classes and Static Class Members

Static classes and class members are used to create data and functions that can be
accessed without creating an instance of the class. Static class members can be used to
separate data and behavior that is independent of any object identity: the data and functions
do not change regardless of what happens to the object. Static classes can be used when there
is no data or behavior in the class that depends on object identity.

Static Classes:
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.

Use a static class to contain methods that are not associated with a particular object.

The main features of a static class are:
1)They only contain static members.
2)They cannot be instantiated.
3)They are sealed.
4)They can have only static constructor


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. For more information,
see Static Constructors (C# Programming Guide).

No comments: