Properties of Static Constructor:
- Static constructor can't have access modifiers like Public,Private, Protected.
- It does not accept parameters.
- 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 can be called directly.
- Static constructor can only access static members.
- The user has no control on when the static constructor is executed in the program.
Ex:
public class MYStaticClass
{
// Static constructor:
static MYStaticClass()
{
System.Console.WriteLine("Static constructor called.");
}
public static void StaticMethod()
{
System.Console.WriteLine("StaticMethod is called.");
}
}
class TestClass
{
private void Test()
{
// Call to static method.
MYStaticClass.StaticMethod();
}
}
//Output:
//Static constructor called.
//StaticMethod is called.
Regards,
Mahesh Bagul
No comments:
Post a Comment