0

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();
}
canton7
  • 37,633
  • 3
  • 64
  • 77
Deepak Jain
  • 109
  • 6
  • Why do you think it should be in that order? Your instance constructors and your inheritance has nothing to do with your static constructors. – ProgrammingLlama Apr 15 '19 at 07:59
  • 3
    In `B`'s static constructor you reference `A.b`, causing `A`'s static constructor to run first. Please read [ask] and show your research. – CodeCaster Apr 15 '19 at 08:01

0 Answers0