Deepak Jain
Deepak Jain

Reputation: 109

why the order of static Constructor is coming like this C#?

i have 3 class
A-->B-->C
all classes have static constructor and normal constructor
now according to me o/p should be like

static C
Static B
static A

A constructor
B constructor
C constructor

but it's coming like this

static C
Static A
static B
A constructor
B constructor
C constructor

can any one pls explain why it's happening
my code

class A
{
    protected static int a,b;
    protected int c;
    static A()
    {
        // a = 10;
        Console.WriteLine("static A");

    }

    public A()
    {
        // a = 5;
        Console.WriteLine("A Constructor");
        // c = 0;
    }
}

class B : A
{
    static B()
    {
        b = 20;
        // a = 30;
        Console.WriteLine("Staic B");
        // b = 20;
        // a = 30;
    }
    public B()
    {
        Console.WriteLine("B Constructor");
        // c = 4;
        // a = 7;
    }
}

class C : B
{
    static C()
    {
        // a = 10;
        Console.WriteLine("Static C");
    }

    public C()
    {
        Console.WriteLine("C Constructor");
        // c = 0;
        // a = 70;
    }
}

static void Main(string[] args)
{
    C c = new C();
    Console.ReadKey();
}

Upvotes: 0

Views: 46

Answers (0)

Related Questions