7.6. Constructor initializers and constructor chaining

Called 'constructor initializers' in C#, the two keywords this (similar to Java's this) and base (similar to Java's super) allow the programmer to invoke an overloaded constructor in the same class and a constructor in the superclass, respectively.

Like Java

  • You can use this() to call an overloaded constructor in the same class by passing into this() the correct parameters. Instead of a separate this() statement, in C#, the this keyword is used like an extension of the constructor declaration as shown in the example below.

     1: using System;
     2:
     3: public class Test{
     4:   public static void Main(){
     5:     Test t = new Test(0);
     6:   }
     7:
     8:   public Test(int i):this("a string"){
     9:     Console.WriteLine("constructor with int param");
    10:   }
    11:
    12:   public Test(string s){
    13:     Console.WriteLine("constructor with string param");
    14:   }
    15: }
    

    Output:

    c:expt>test
    constructor with string param
    constructor with int param
    

    The this keyword is used on line 8 so that when this constructor is invoked, it first invokes the overloaded constructor which takes in a string parameter (on lines 12 – 14).

  • You can use base() to call a specific constructor in the superclass by passing into base() the correct parameters which match one of the constructors in the superclass. Like this(), base() is an extension of the constructor's method declaration. Examine this example.

     1: using System;
     2:
     3: public class Child:Parent{
     4:   public static void Main(){
     5:     Child c = new Child(0);
     6:   }
     7:
     8:   public Child(int i):base("hello!"){
     9:     Console.WriteLine("Child with int param");
    10:   }
    11: }
    12:
    13: public class Parent{
    14:   public Parent(string s){
    15:     Console.WriteLine("Parent with string param");
    16:   }
    17:   public Parent(){
    18:     Console.WriteLine("Parent with no param");
    19:   }
    20: }
    

    Output:

    c:expt>test
    Parent with string param
    Child with int param
    

    Constructor chaining up the inheritance hierarchy, as seen in Java, still happens in C#. If none of the superclass's constructor is explicitly invoked via the base() extension of the method declaration, the constructor of a subclass will always invoke the default constructor [8] of the immediate superclass before executing the rest of its statements, as this example shows:

    [8] The default constructor is the constructor with no parameters.

     1: using System;
     2:
     3: public class Child:Parent{
     4:   public static void Main(){
     5:     Child c = new Child(0);
     6:   }
     7:
     8:   public Child(int i){ // invoked last
     9:     Console.WriteLine("Child with 1 param");
    10:   }
    11: }
    12:
    13: public class Parent:GrandParent{
    14:   public Parent(){ // invoked 2nd
    15:     Console.WriteLine("Parent");
    16:   }
    17: }
    18:
    19: public class GrandParent{
    20:   public GrandParent(){ // invoked 1st
    21:     Console.WriteLine("GrandParent");
    22:   }
    23: }
    

Output:

c:expt>test
GrandParent
Parent
Child with 1 param

Unlike Java

The differences between Java's this() and super() compared to C#'s this() and base() are largely syntactical when used in constructors. In Java, you can only use this() and super() as a statement in the first line of the constructor, if they are used. In C#, the keywords are extensions of the constructor's method declaration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.9.148