Wednesday, 5 September 2012

Static Class in C#


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.
A Static class have these properties......
1.    Static class can’t be instantiated.
2.    Static class can contain only static members. It does not have any non static members
3.    Static class can’t contain Instance Constructor, because if any static constructor contain instance constructor then it will callable when any instance of the class is created.
For example:

public ClassName(int x, int y)
{
    this.x = x;
    this.y = y;
}

And this constructor will callable when it will be call as like this
This allows CoOrd objects to be created with default or specific initial values, like this:

ClassNamep2 = new ClassName(5, 3); // Static class does not allow to make an instance of the class. That’s why static class does not have any instance constructor.

4.    Static class does not inherited by any other derived class. Because they are implicitly sealed.

No comments:

Post a Comment