#290 – Chaining Constructors

You can have one constructor in a class call another.  This is known as constructor chaining.

With constructor chaining, one constructor calls another to help it initialize the data in the class.

As an example, assume that we have a Dog class with two constructors, one which accepts name and age parameters, and one which accepts only a name parameter.

In the example below, the constructor that takes two arguments initializes both name and age properties.  The constructor that takes only a name parameter chains to the first constructor, passing it the name parameter and a default value for age.  This first constructor is called using the this keyword.

    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public Dog(string name)
            : this(name, 1)
        {
        }
    }

#289 – You Can Define Multiple Constructors

We can define multiple constructors in a class,  each one taking a different set of parameters.

Here’s an example where we define four different constructors for a Dog object.

        public string Name { get; set; }
        public int Age { get; set; }
        public string Motto { get; set; }

        public Dog(string name)
        {
            Name = name;
            Age = 1;
            Motto = "Happy";
        }

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
            Motto = "Happy";
        }

        public Dog(string name, string motto)
        {
            Name = name;
            Motto = motto;
            Age = 1;
        }

        public Dog(string name, int age, string motto)
        {
            Name = name;
            Age = age;
            Motto = motto;
        }

We now have four different ways to construct a Dog object.

            Dog d1 = new Dog("Kirby");                        // name
            Dog d2 = new Dog("Jack", 16);                     // name, age
            Dog d3 = new Dog("Ruby", "Look out window");      // name, motto
            Dog d4 = new Dog("Lassie", 71, "Rescue people");  // name, age, motto

#288 – Passing Arguments to a Constructor

You can define a constructor in a class that takes one or more arguments.  Typically, these represent data to be used in initializing the new object.

Here’s an example of a constructor for the Dog class that accepts the dog’s name and age and then assigns those values to the corresponding properties.

        public string Name { get; set; }
        public int Age { get; set; }

        // Constructor that takes dog's name and age
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

Having this constructor, we can instantiate a new Dog instance as follows:

            Dog kirby = new Dog("Kirby", 14);

#287 – You Don’t Have to Define a Constructor

It’s not mandatory for a user-defined class to define a constructor.  If one is not defined, the compiler will automatically generate a constructor. This internal constructor will just call the constructor of the class’ base class.  (E.g. The constructor in System.Object).

You can use the IL DASM tool to inspect the code for your class and see this automatically generated constructor.

For example, let’s say that we have a Dog class that does not define a constructor.  Below is an image of the IL DASM, showing the metadata for the Dog class.  Note that it shows the following elements in the class:

  • Age and Name properties
  • get and set accessors for the properties
  • Backing variables for the properties
  • A Bark method
  • A method called .ctor–this is the automatically-generated constructor

This constructor just calls the System.Object constructor: