Constructor and Destructor – C# Programming Questions – Real

Q- Write code for constructor in Derived class Y that calls base class X’s constructor.

class X
{     
   public X(int a) { Console.WriteLine("X:ctor"); }
}

class Y : X
{   
   //Write constructor here that calls X's constructor    
}

Answer:

class Y : X
{   
    public Y(int a):base(a) 
    { Console.WriteLine("Y:ctor"); }     
}

Q- What is output of this program?

class Car
{
    public Car(int a)
    {
        Console.WriteLine("Car: ctr");
    }
    public void func(){
        Console.WriteLine("func");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Car c = new Car();
        c.func();
    }   
}

Answer: Compiler Error.
Explaination: At statement Car c = new Car(); compiler will look for the constructor with no parameter that is not available in the class. Hence, compiler error.
If we create any parameterized constructor in a class, compiler will not provide default constructor.So, as a solution, we need to put a constructor with no parameter e.g.

class Car
{
    public Car() {   }// Empty parameter constructor
    public Car(int a)
    {
        Console.WriteLine("Car: ctr");
    }
    public void func(){
        Console.WriteLine("func");
    }
}

NOTES: In below class we don’t have written any constructor, So, compiler will provide default constructor. And main program will work properly.

class Car
{  
    public void func(){
        Console.WriteLine("func");
    }
}
static void Main(string[] args)
    {
        Car c = new Car();
        c.func();
    }  

Q- What is output of below program?

class A
{
   public A() { Console.WriteLine("Constructor: class A"); }
   ~A() { Console.WriteLine("Destructor: class A"); }
}
//Derived class
class B : A
{
    public B() { Console.WriteLine("Constructor: class B"); }
     ~B() { Console.WriteLine("Destructor: class B"); }
    
}
//Derived class
class C : B
{
    public C() { Console.WriteLine("Constructor: class C"); } 
    ~C() { Console.WriteLine("Destructor: class C"); }
}



class Program
{
    static void Main(string[] args)
    {

        A o1 = new B();     

        A o2 = new C();        

    }
}

Answer: Output:

Constructor: class A
Constructor: class B
Constructor: class A
Constructor: class B
Constructor: class C
Destructor: class C
Destructor: class B
Destructor: class A
Destructor: class B
Destructor: class A

Explanation: Note that constructors are called from base to derived class and destructors in reverse order. For easy understanding read below comments.

static void Main(string[] args)
    {
        //On below statement execution, constructor of base class A
        //will be called first then of class B.

        //Constructor: class A
        //Constructor: class B

        A o1 = new B(); //Statement - 1

        //On below statement execution, constructor of base class A
        //will be called first then of class B then of class C.

        //Constructor: class A
        //Constructor: class B
        //Constructor: class C

        A o2 = new C(); //Statement - 2   
    
        
        //Now, desturctors for statment -2 will be called. i.e.
        //Destructor of class C, destructor of class B then
        //Destructor of class A.

        //Destructor: class C
        //Destructor: class B
        //Destructor: class A


        //Now, Destructors for Statement -1 will be called i.e.
        //destructor of class B then Destructor of class A.
        
        //Destructor: class B
        //Destructor: class A

    }

Related Posts