Posts Tagged ‘static classes’

Facts about static classes and constructors

Static Classes

 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.

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.

For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

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. 

 When to Use Static Classes

When methods do not need to be attached to a specific instance of the class.

 Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

Static Members

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

  • Static class members are declared using the static keyword before the return type of the member.
  • Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called.
  • To access a static class member, use the name of the class instead of a variable name to specify the location of the member.

 

 Static constructors 

Static constructors have the following properties:

  •  A static constructor does not take access modifiers or have parameters. A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only.
  • It is called automatically before the first instance is created or any static members are referenced.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  •  A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  •  A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  •  Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

 

Reference Online MSDN

Refer this link for more detail http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

Advertisements