Open In App

Constructor Chaining in C#

Last Updated : 13 Sep, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

Constructor chaining in C# means calling one constructor from another within the same class or from a base class. It helps reuse initialization code and keeps the class clean by avoiding repetition. Constructor chaining is done using the keywords this and base.

  • Constructor chaining allows reusing existing constructor logic.
  • this is used to call another constructor in the same class, while base is used to call a constructor of the parent class.
  • Chaining must be the first statement inside the constructor.
  • Helps avoid code duplication and makes initialization consistent.

1. Chaining with this Keyword

When a class has multiple constructors, one constructor can call another in the same class using this.

C#
using System;

class Student {
    string name;
    int age;

    // Constructor with one parameter
    public Student(string name) {
        this.name = name;
        Console.WriteLine("Name: " + name);
    }

    // Constructor with two parameters, chaining to first
    public Student(string name, int age) : this(name) {
        this.age = age;
        Console.WriteLine("Age: " + age);
    }

    public static void Main() {
        Student s1 = new Student("Amit");
        Student s2 = new Student("Sita", 21);
    }
}

Output:

Name: Amit
Name: Sita
Age: 21

2. Chaining with base Keyword

If a class inherits another class, a constructor in the child class can call the constructor of the base class using base.

C#
using System;

class Person {
    // Base class constructor
    public Person(string name) {
        Console.WriteLine("Person Constructor: " + name);
    }
}

class Employee : Person {
    // Derived class constructor chaining to base class constructor using 'base'
    public Employee(string name, int id) : base(name) {
        Console.WriteLine("Employee Constructor: Id = " + id);
    }

    public static void Main() {
        Employee emp = new Employee("Rahul", 101);
    }
}

Output:

Person Constructor: Rahul
Employee Constructor: Id = 101

3. Multiple Levels of Chaining

Constructors can chain across multiple levels in the hierarchy, ensuring initialization happens in order.

C#
using System;

class A {
    // Base class constructor
    public A() {
        Console.WriteLine("Class A Constructor");
    }
}

class B : A {
    // Derived class B constructor chaining to base class A
    public B() : base() {
        Console.WriteLine("Class B Constructor");
    }
}

class C : B {
    // Derived class C constructor chaining to base class B
    public C() : base() {
        Console.WriteLine("Class C Constructor");
    }

    public static void Main() {
        // Creating object of C triggers constructors in order: A -> B -> C
        C obj = new C();
    }
}

Output:

Class A Constructor
Class B Constructor
Class C Constructor

Rules for Constructor Chaining

  1. Chaining must be the first statement in the constructor body.
  2. Only one constructor can be chained at a time.
  3. You can use this for same class chaining or base for parent class chaining.
  4. If no constructor is explicitly chained, the compiler calls the default base constructor automatically.

Note: Constructor chaining must not be circular, calling one constructor that eventually calls itself will cause a compile-time error.

Suggested Quiz
4 Questions

Which keyword is used to call another constructor within the same class?

  • A

    base

  • B

    this

  • C

    super

  • D

    parent

Explanation:


What will be the order of constructor calls when creating an object of class C?

Java
class A { public A() { Console.WriteLine("A"); } }
class B : A { public B() { Console.WriteLine("B"); } }
class C : B { public C() { Console.WriteLine("C"); } }


  • A

    C → B → A

  • B

    A → C → B

  • C

    B → A → C

  • D

    A → B → C

Explanation:


What happens if a constructor tries to chain to another constructor in such a way that it eventually calls itself?

  • A

    It runs normally

  • B

    It causes infinite runtime loop

  • C

    It causes a compile-time error

  • D

    It automatically resolves the cycle

Explanation:


Which of the following statements about constructor chaining in C# is true?

  • A

    The this keyword is used to call a constructor of the base class.

  • B

    Constructor chaining can appear anywhere inside the constructor body.

  • C

    The base keyword is used to call a constructor of the parent class.

  • D

    Constructor chaining is optional but must be used in every constructor.

Explanation:


Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Article Tags :

Explore