Demonstrate when constructors are called. : Class hierarchy : Class C# Examples


C# Examples » Class » Class hierarchy »

 

Demonstrate when constructors are called.





The general form of calling base constructor:




    
derived-constructor(parameter-list)  :  base(arg-list)  {
        //  body  of  constructor
}
    
   
  
   





    
using  System;  
  
//  Create  a  base  class.  
class  BaseClass  {  
    public  BaseClass()  {    
        Console.WriteLine("Constructing  BaseClass.");  
    }  
}  
  
//  Create  a  class  derived  from  BaseClass.  
class  DerivedClass  :  BaseClass  {  
    public  DerivedClass()  {  
        Console.WriteLine("Constructing  DerivedClass.");  
    }  
}  
  
//  Create  a  class  derived  from  DerivedClass.  
class  DerivedDerivedClass  :  DerivedClass  {  
    public  DerivedDerivedClass()  {  
        Console.WriteLine("Constructing  DerivedDerivedClass.");  
    }  
}  
  
class  MainClass  {  
    public  static  void  Main()  {  
        DerivedDerivedClass  c  =  new  DerivedDerivedClass();  
    }  
}
    
   
  
   



Output

Constructing BaseClass.
Constructing DerivedClass.
Constructing DerivedDerivedClass.


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Class hierarchy