0
0
C Sharp (C#)programming~5 mins

How constructor chaining works in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Constructor chaining helps you reuse code when creating objects. It lets one constructor call another to avoid repeating setup steps.

When you have multiple ways to create an object but some steps are common.
When you want to keep your code clean and avoid repeating initialization code.
When you want to provide default values but also allow custom values.
When you want to organize constructors from simple to complex setups.
Syntax
C Sharp (C#)
public class ClassName
{
    public ClassName() : this(default(Type))
    {
        // This constructor calls the other constructor
    }

    public ClassName(Type parameter)
    {
        // Initialization code here
    }
}

The : this(...) syntax calls another constructor in the same class.

Constructor chaining must be the first statement in the constructor.

Examples
This example shows a default constructor calling a more detailed constructor with default values.
C Sharp (C#)
public class Person
{
    public string Name;
    public int Age;

    public Person() : this("Unknown", 0)
    {
        // Calls the constructor with two parameters
    }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}
This example shows the default and single-parameter constructors both chaining to the two-parameter constructor.
C Sharp (C#)
public class Box
{
    public int Width;
    public int Height;

    public Box() : this(1, 1)
    {
        // Default size box
    }

    public Box(int size) : this(size, size)
    {
        // Square box
    }

    public Box(int width, int height)
    {
        Width = width;
        Height = height;
    }
}
If there is only one constructor, chaining is not needed.
C Sharp (C#)
public class EmptyExample
{
    public EmptyExample() { }
}
Sample Program

This program shows constructor chaining in action. Each constructor calls the next one with more details. The output shows which constructor runs and the final car info.

C Sharp (C#)
using System;

public class Car
{
    public string Make;
    public string Model;
    public int Year;

    public Car() : this("Unknown", "Unknown", 0)
    {
        Console.WriteLine("Default constructor called.");
    }

    public Car(string make, string model) : this(make, model, 2024)
    {
        Console.WriteLine("Constructor with make and model called.");
    }

    public Car(string make, string model, int year)
    {
        Make = make;
        Model = model;
        Year = year;
        Console.WriteLine("Constructor with make, model, and year called.");
    }

    public void PrintInfo()
    {
        Console.WriteLine($"Car: {Make}, Model: {Model}, Year: {Year}");
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Creating car1 with default constructor:");
        Car car1 = new Car();
        car1.PrintInfo();

        Console.WriteLine();

        Console.WriteLine("Creating car2 with make and model constructor:");
        Car car2 = new Car("Toyota", "Corolla");
        car2.PrintInfo();

        Console.WriteLine();

        Console.WriteLine("Creating car3 with make, model, and year constructor:");
        Car car3 = new Car("Honda", "Civic", 2020);
        car3.PrintInfo();
    }
}
OutputSuccess
Important Notes

Constructor chaining helps reduce repeated code and keeps constructors organized.

Time complexity is the same as normal constructors; it just calls another constructor.

Common mistake: forgetting that chaining must be the first line in the constructor.

Use constructor chaining when you want to provide multiple ways to create an object but share common setup code.

Summary

Constructor chaining lets one constructor call another in the same class.

It helps avoid repeating code and keeps initialization consistent.

Use : this(...) syntax as the first line in a constructor to chain.