Constructors and Destructors


Constructors and destructors are not inherited. However, both the bases and derived components of an object require initialization. The base component of an object is responsible for initializing base members, whereas the derived component is responsible for initializing derived members. Constructors of both the derived and base components are called for this reason. Because constructors are not inheritable, derived types must implement local constructors to initialize derived members.

During the instantiation of a derived type, the default or parameterless instance constructor of the based type is called by default. If the base class does not have a default constructor, a compile error occurs unless the derived class calls a constructor in the base class that has a parameter. In the following code, ZClass does not have a default constructor and will not compile:

 public class ZClass {     public ZClass(int param) {     } } public class YClass: ZClass { } 

Constructors in a derived class can explicitly call constructors in the base class. This is particularly useful for calling parameterized constructors in the base class. Base class constructors are called from the derived class using a constructor initializer list. The initializer list can only be affixed to instance constructors in the derived type. The derived constructor is responsible for initializing the derived type and possibly calling the base class constructor.

This is the constructor initializer syntax:

  • accessibility modifier typename(parameterlist1) : base(parameterlist2)

  • {constructorbody}

The constructor initializer list follows the derived constructor name and colon. The base keyword refers to the base class constructor. The parameterlist2 determines which base class constructor is called. Parameters of the derived constructor can be used in the parameter list of the base class constructor.

In the following example, the YClass constructor explicitly calls the one-argument constructor of the base class:

 public class ZClass { public ZClass(int param) {     } } public class YClass: ZClass { public YClass(int param) : base(param) {     } } 

Instances of a derived type are created inside out. The base element is created first, and then the derived elements. In support of this model, the constructors are walked bottom-up. This facilitates passing parameters from derived constructors to base constructors. After the parameters are passed up the class hierarchy, the constructors are executed top-down, beginning with the constructor in the root class.

The following code confirms that constructor initializers are called bottom-up but the constructors are invoked top-down starting with the constructor in the base class:

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             XClass obj=new XClass();         }     }     public class ZClass {         public ZClass(int param) {             Console.WriteLine("ZClass constructor");         }     }     public class YClass: ZClass {         public YClass(int param) : base(YClass.MethodA()) {             Console.WriteLine("YClass constructor");         }         public static int MethodA() {             Console.WriteLine("YClass constructor initializer");             return 0;         }     }     public class XClass: YClass {         public XClass() : base(XClass.MethodA()) {             Console.WriteLine("XClass constructor");         }         public static new int MethodA() {             Console.WriteLine("XClass constructor initializer");             return 0;         }     } } 

This is the output from the code, which confirms the sequencing of base constructor initializers and the actual invocation of constructors:

 C:\>insideout XClass constructor initializer YClass constructor initializer ZClass constructor YClass constructor XClass constructor 

Objects are not fully created until the constructor has finished completely. Therefore, the this reference of the derived type cannot be used as a parameter in the base class constructor initializer list:

 public class ZClass {   public ZClass(YClass obj) {   } } public class YClass: ZClass {   public YClass() : base(this) { // Illegal   } } 

Destructors are called in reverse order of constructors. Derived objects are destroyed outside-in, where the most-derived component is destroyed first. Correspondingly, destructors are called bottom-up. Because destructors are parameterless, there is no information to pass between them. In C#, base class destructors are called automatically. Do not attempt to call the base class destructor explicitly.

The following code has three classes—each with a destructor—which form a class hierarchy. At the end of the program, the destructors are called bottom-up, which confirms the sequencing of destructors.

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             XClass obj=new XClass();         }     }     public class ZClass {         ~ZClass() {             Console.WriteLine("ZClass destructor");         }     }     public class YClass: ZClass {         ~YClass() {             Console.WriteLine("YClass destructor");         }     }     public class XClass: YClass {         ~XClass() {             Console.WriteLine("XClass destructor");         }     } } 

This is the output from the program:

 C:\>destructors XClass destructor YClass destructor ZClass destructor 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net