Saturday, January 3, 2009

Exploring Static Classes and Static Class Members

To start with this topic I will say static class is mostly same as a non-static class, but having one major difference that a static class cannot be instantiated. In other words, I will say you cannot use the new keyword to create a variable of the static class type. Because there is no instance variable which needs to be initialized, you simply access the members of a static class by using the class name itself.

Since the type information for a static class is loaded by the .NET Framework CLR when the program that references the class is loaded. The program cannot specify exactly or will not guaranteed when the class is loaded. However, it is guaranteed that it will be loaded and to have its fields initialized and its static constructor will be called before the class is referenced for the first time in your program. A static constructor is only called or invoked one time, and a static class remains in memory througout or for the lifetime of the application domain in which executed program resides.


Let's in a few words draw the basic features of a static class:
*It contains only static members.
*It cannot be instantiated. (Object can not be created)
*It is also sealed. (Can not be inherited)
*It cannot contain instance constructors.(So that members will not be initialized)

Creating a static class is basically the same as creating a class that contains only static members and a private constructor, As 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 and it will guarantee that instances of this class cannot be created.

Static classes are sealed i.e they cannot be inherited. They cannot inherit from any class except Object. Static classes can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

A non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created. Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.


It is more emblematic to declare a non-static class with some static members, than to declare an entire class as static. Basically there are two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances.

Static methods can be overloaded but not overridden, As they belong to the class, and not to any instance of the class. Although a field cannot be declared as static const, a const field is essentially static in its behavior. It belongs to the type, not to instances of the type. Therefore, const fields can be accessed by using the same ClassName.MemberName notation that is used for static fields. No object or instance is required.

As C# does not support static local variables (variables that are declared in method scope or have local scope). So it will be fair to declare static class members by using the static keyword before the return type of the member, as shown in the following example:


public class Bike{
public static int NoOfTyres = 2;
public static int SizeofFuelTank
{
get
{
return 13;
}
}
public static void Run() { }
public static event EventType RunOutOfFuel;
//Declare other non-static members..
}

No comments:

Post a Comment