Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
C Obj = new C();
C.C1();
Console.ReadKey();
}
}
public static class DHINESH
{
static DHINESH()
{
DHINESH1();
}
static void DHINESH1()
{
}
}
public class A
{
public A()
{
Console.WriteLine("This is A");
}
static A()
{
Console.WriteLine("This is static A");
}
}
public class B : A
{
public B()
{
Console.WriteLine("This is B");
}
static B()
{
Console.WriteLine("This is static B");
}
}
public class C : B
{
public C()
{
Console.WriteLine("This is C");
}
static C()
{
Console.WriteLine("This is static C");
}
public static void C1()
{
}
}
}
Result:
-->When we instantiate a Class like this C Obj = new C();
The output is :
This is static C
This is static B
This is static A
This is A
This is B
This is C
-->When we instantiate a Class like this
C Obj = new C();
C Obj1 = new C();
The output is :
This is static C
This is static B
This is static A
This is A
This is B
This is C
This is A
This is B
This is C
-->When we call a static method like this C.C1();
The output is:
This is static C
Limitations of Static constructor:
1. We cannot specify access modifier.
2. We cannot use any parameter.
3. Static constructor will be executing only once and also very first instance of the class.
4. When any static method of the class is being called the static constructor will be executing at first call itself.
5.We can use only static members or variables inside the static constructor.
No comments:
Post a Comment