Pages

Men

rh

7/01/2012

Static classes in C#

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).
      
The main features of a static class are:
  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed. 
  • They cannot contain Instance Constructors
  • 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.
  • 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.

No comments :

Post a Comment