Bird
Raised Fist0
C Sharp (C#)programming~20 mins

How constructor chaining works in C Sharp (C#) - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Constructor Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constructor chaining with base and this
What is the output of this C# program that uses constructor chaining with this and base?
C Sharp (C#)
using System;
class BaseClass {
    public BaseClass() {
        Console.WriteLine("BaseClass default constructor");
    }
    public BaseClass(string msg) {
        Console.WriteLine("BaseClass says: " + msg);
    }
}
class DerivedClass : BaseClass {
    public DerivedClass() : this("Hello") {
        Console.WriteLine("DerivedClass default constructor");
    }
    public DerivedClass(string msg) : base(msg) {
        Console.WriteLine("DerivedClass says: " + msg);
    }
}
class Program {
    static void Main() {
        var obj = new DerivedClass();
    }
}
A
BaseClass default constructor
DerivedClass says: Hello
DerivedClass default constructor
B
BaseClass says: Hello
DerivedClass says: Hello
DerivedClass default constructor
C
BaseClass says: Hello
DerivedClass default constructor
DerivedClass says: Hello
D
BaseClass default constructor
DerivedClass default constructor
DerivedClass says: Hello
Attempts:
2 left
💡 Hint
Remember that this calls another constructor in the same class before executing the body, and base calls the base class constructor.
Predict Output
intermediate
2:00remaining
Constructor chaining order in inheritance
What will be printed when this C# program runs?
C Sharp (C#)
using System;
class A {
    public A() {
        Console.WriteLine("A()");
    }
    public A(int x) {
        Console.WriteLine("A(int)");
    }
}
class B : A {
    public B() : base(5) {
        Console.WriteLine("B()");
    }
    public B(int x) : this() {
        Console.WriteLine("B(int)");
    }
}
class Program {
    static void Main() {
        var b = new B(10);
    }
}
A
A(int)
B()
B(int)
B
A()
B()
B(int)
C
A(int)
B(int)
B()
D
A()
B(int)
B()
Attempts:
2 left
💡 Hint
Check which constructors call which and in what order base constructors run.
🔧 Debug
advanced
2:00remaining
Identify the error in constructor chaining
What error will this C# code produce when compiled?
C Sharp (C#)
class Test {
    public Test() : this(5) {
    }
    public Test(int x) : this() {
    }
}
ANo error, compiles and runs fine
BCompile-time error: Missing base constructor call
CCompile-time error: Constructor chaining causes a cycle
DRuntime error: Stack overflow
Attempts:
2 left
💡 Hint
Check if constructors call each other in a loop.
Predict Output
advanced
2:00remaining
Output with multiple constructor chaining levels
What is the output of this C# program?
C Sharp (C#)
using System;
class X {
    public X() {
        Console.WriteLine("X()");
    }
    public X(string s) : this() {
        Console.WriteLine("X(string): " + s);
    }
}
class Y : X {
    public Y() : base("hello") {
        Console.WriteLine("Y()");
    }
    public Y(int n) : this() {
        Console.WriteLine("Y(int): " + n);
    }
}
class Program {
    static void Main() {
        var y = new Y(10);
    }
}
A
X()
X(string): hello
Y()
Y(int): 10
B
X(string): hello
X()
Y()
Y(int): 10
C
X()
Y()
X(string): hello
Y(int): 10
D
X()
Y(int): 10
X(string): hello
Y()
Attempts:
2 left
💡 Hint
Follow the constructor calls from Y(int) down to base.
🧠 Conceptual
expert
2:00remaining
Why constructor chaining is useful
Which of the following best explains why constructor chaining is useful in C#?
AIt prevents any constructor from calling base class constructors explicitly.
BIt allows multiple constructors to run in parallel threads for faster object creation.
CIt automatically generates default values for all fields without writing code.
DIt avoids code duplication by letting one constructor reuse another's initialization logic.
Attempts:
2 left
💡 Hint
Think about how constructors can share code.

Practice

(1/5)
1. What does constructor chaining in C# allow you to do?
easy
A. Override a constructor in a derived class
B. Call a method from a constructor
C. Create multiple instances of a class at once
D. Call one constructor from another constructor in the same class

Solution

  1. Step 1: Understand constructor chaining concept

    Constructor chaining means one constructor calls another constructor within the same class to reuse code.
  2. Step 2: Identify what constructor chaining does

    It helps avoid repeating initialization code by calling another constructor using : this(...) syntax.
  3. Final Answer:

    Call one constructor from another constructor in the same class -> Option D
  4. Quick Check:

    Constructor chaining = calling another constructor [OK]
Hint: Constructor chaining calls another constructor in the same class [OK]
Common Mistakes:
  • Confusing constructor chaining with method calls
  • Thinking it creates multiple objects
  • Mixing it up with inheritance or overriding
2. Which of the following is the correct syntax to chain constructors in C#?
easy
A. public MyClass() : base() { }
B. public MyClass() : this() { }
C. public MyClass() : this(5) { }
D. public MyClass() { this(); }

Solution

  1. Step 1: Recall constructor chaining syntax

    Constructor chaining uses : this(parameters) after the constructor signature to call another constructor in the same class.
  2. Step 2: Analyze options

    public MyClass() : this(5) { } uses : this(5) which correctly calls another constructor with an int parameter. public MyClass() : this() { } calls itself recursively causing error. public MyClass() : base() { } calls base class constructor, not chaining. public MyClass() { this(); } tries to call constructor inside body, which is invalid.
  3. Final Answer:

    public MyClass() : this(5) { } -> Option C
  4. Quick Check:

    Constructor chaining syntax = : this(...) [OK]
Hint: Use ': this(...)' after constructor signature to chain [OK]
Common Mistakes:
  • Using 'this()' inside constructor body instead of after signature
  • Confusing base() with this()
  • Calling the same constructor recursively
3. What will be the output of this C# code?
class Test {
  public Test() : this(10) {
    Console.WriteLine("Default constructor");
  }
  public Test(int x) {
    Console.WriteLine($"Constructor with {x}");
  }
}

class Program {
  static void Main() {
    Test t = new Test();
  }
}
medium
A. Constructor with 10 Default constructor
B. Constructor with 10
C. Default constructor Constructor with 10
D. Default constructor

Solution

  1. Step 1: Understand constructor chaining call order

    The parameterless constructor calls this(10), so the constructor with int parameter runs first.
  2. Step 2: Trace output order

    First, "Constructor with 10" is printed from the int constructor. Then control returns to parameterless constructor which prints "Default constructor".
  3. Final Answer:

    Constructor with 10 Default constructor -> Option A
  4. Quick Check:

    Chained constructor runs first, then caller prints [OK]
Hint: Chained constructor runs before the calling constructor body [OK]
Common Mistakes:
  • Assuming calling constructor runs first
  • Ignoring constructor chaining order
  • Mixing output order
4. Identify the error in this constructor chaining code:
class Sample {
  public Sample() : this() {
    Console.WriteLine("Hello");
  }
}
medium
A. Missing base keyword for chaining
B. Recursive constructor call causing infinite loop
C. Constructor chaining syntax is correct
D. Constructor must have a return type

Solution

  1. Step 1: Analyze constructor chaining call

    The constructor calls itself with : this(), causing infinite recursion.
  2. Step 2: Identify error type

    This recursive call leads to a runtime stack overflow error because no termination occurs.
  3. Final Answer:

    Recursive constructor call causing infinite loop -> Option B
  4. Quick Check:

    Constructor calling itself = infinite recursion [OK]
Hint: Avoid chaining a constructor to itself directly [OK]
Common Mistakes:
  • Thinking chaining to self is allowed
  • Confusing base() and this() calls
  • Expecting constructor to have return type
5. Given this class, what will be the output when creating new Box()?
class Box {
  public int Width, Height;
  public Box() : this(5, 10) {
    Console.WriteLine("Default Box");
  }
  public Box(int w, int h) {
    Width = w;
    Height = h;
    Console.WriteLine($"Box: {Width}x{Height}");
  }
}
hard
A. Box: 5x10 Default Box
B. Default Box Box: 5x10
C. Box: 0x0 Default Box
D. Default Box

Solution

  1. Step 1: Understand constructor chaining and initialization

    The parameterless constructor calls this(5, 10), so the two-parameter constructor runs first, setting Width and Height and printing their values.
  2. Step 2: Trace output order

    First, "Box: 5x10" is printed from the two-parameter constructor. Then control returns to the parameterless constructor which prints "Default Box".
  3. Final Answer:

    Box: 5x10 Default Box -> Option A
  4. Quick Check:

    Chained constructor runs first, then caller prints [OK]
Hint: Chained constructor runs before caller's body executes [OK]
Common Mistakes:
  • Assuming default values 0 for Width and Height
  • Thinking default constructor runs first
  • Ignoring chaining call order