15

I was just reading Inheritance in C# in which i came across the Constructors and was written that constructors are executed in order of derivation. What does it mean?That base class constructor will be called first or Derived class.

RBT
  • 24,161
  • 21
  • 159
  • 240
Eljay
  • 941
  • 5
  • 15
  • 30

4 Answers4

22

A base class Constructor is called first.Refer to the following example

// Demonstrate when constructors are called.
using System;

// Create a base class.
class A {
    public A() {
        Console.WriteLine("Constructing A.");
    }
}

// Create a class derived from A.
class B : A {
    public B() {
        Console.WriteLine("Constructing B.");
    }
}

// Create a class derived from B.
class C : B {
    public C() {
        Console.WriteLine("Constructing C.");
    }
}

class OrderOfConstruction {
    static void Main() {
        C c = new C();
    }
}

The output from this program is shown here:

Constructing A.
Constructing B.
Constructing C.
Scott Smith
  • 1,823
  • 17
  • 15
Junior Bill gates
  • 1,858
  • 4
  • 20
  • 33
3

The base class constructor will be called first. You can test this pretty easily yourself:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DerivationTest
{
    class Program
    {
        public class Thing
        {
            public Thing()
            {
                Console.WriteLine("Thing");
            }
        }

        public class ThingChild : Thing
        {
            public ThingChild()
            {
                Console.WriteLine("ThingChild");
            }
        }

        static void Main(string[] args)
        {
            var tc = new ThingChild();
            Console.ReadLine();
        }
    }
}
LiquidPony
  • 2,188
  • 1
  • 17
  • 19
3

Class constructors are called in the order implied by derivation, but it's important to note that in C#, field initializers (e.g. int foo=5) are run before the base class constructor, and thus run in the opposite order. This is primarily useful if the base constructor may cause virtual function which is overridden in the derived class to be called before the base constructor has completed. Such a function will see any variables that were initialized by field initializers as having been initialized.

VB.net, incidentally, runs all field initializers after the base class constructor completes, but before anything in the derived class constructor (other than the chain to the base constructor). This means that virtual methods have to be aware that field initializers may not have run, but also means that field initializers can make use of the object under constructions (which they cannot do in C#).

Incidentally, in VB.net, it's possible, though clunky, for a class to safely initialize fields with newly-created IDisposable instances (and ensure they will get disposed if the any part of the construction process throws an exception). In C#, one must refrain from using field initializers to create anything that cannot be safely abandoned if construction throws an exception, since there will be no way to access the partially-constructed object to clean it up.

supercat
  • 77,689
  • 9
  • 166
  • 211
0
using System;

class Parent 
 {
    public Parent () {
    Console.WriteLine("Hey Its Parent.");
  }
}

class Derived : Parent 
  {
     public Derived () {
    Console.WriteLine("Hey Its Derived.");
   }
}

class OrderOfExecution {
      static void Main() {
        Derived obj = new Derived();
     }
}

The output from this program is shown here:

Hey Its Parent.

Hey Its Derived.

Constructor acts differently in Inheritance bit confusing for new Programmers. There are two concepts in execution of constructor 1. Calling 2. Execution When you create a object of your derived class Named Derived the constructor first goes to Derived() then it goes to Parent() because of its calling. Constructor calling is done from Bottom to Top but then you find it Executes Parent() first then Derived , that is because of its Execution. Constructors Executed from Top to Bottom. That's why it print Parent first then Base while base constructor called first.

Jatin Phulera
  • 151
  • 1
  • 6