0Pricing
C# Academy · Lesson

Constructor Chaining with this

Reuse initialization across constructors.

Constructor Chaining with this is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Chain Constructors?

When several constructors share setup logic, you can chain them so one calls another. This avoids duplicating code and keeps initialization in one place.

using System;

class Box
{
    public int Width;
    public int Height;

    public Box(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public Box() : this(1, 1) { }
}

class Program
{
    static void Main()
    {
        var def = new Box();
        Console.WriteLine(def.Width + "x" + def.Height);
    }
}

The : this(...) Syntax

Chaining uses : this(args) after the constructor signature. The targeted constructor runs first, then the current body runs.

using System;

class Person
{
    public string Name;
    public int Age;

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
        Console.WriteLine("Full constructor ran");
    }

    public Person(string name) : this(name, 0)
    {
        Console.WriteLine("Name-only constructor ran");
    }
}

class Program
{
    static void Main()
    {
        var p = new Person("Sam");
        Console.WriteLine(p.Name + ", " + p.Age);
    }
}

Order of Execution

The chained constructor executes before the calling constructor body. This guarantees base setup happens first.

using System;

class Step
{
    public Step(int n)
    {
        Console.WriteLine("Init with " + n);
    }

    public Step() : this(0)
    {
        Console.WriteLine("Then default body");
    }
}

class Program
{
    static void Main()
    {
        var s = new Step();
    }
}

A Single Source of Truth

Funnel all constructors into one "primary" constructor that holds the real logic. The others just supply defaults and chain to it.

using System;

class Connection
{
    public string Host;
    public int Port;

    public Connection(string host, int port)
    {
        Host = host;
        Port = port;
    }

    public Connection(string host) : this(host, 8080) { }
    public Connection() : this("localhost", 8080) { }
}

class Program
{
    static void Main()
    {
        var c = new Connection("example.com");
        Console.WriteLine(c.Host + ":" + c.Port);
    }
}

Chaining with Defaults

Chaining is a clean way to express layered defaults: each shorter constructor fills in one more default and delegates.

using System;

class Coffee
{
    public string Size;
    public bool Milk;
    public int Sugar;

    public Coffee(string size, bool milk, int sugar)
    {
        Size = size;
        Milk = milk;
        Sugar = sugar;
    }

    public Coffee(string size, bool milk) : this(size, milk, 0) { }
    public Coffee(string size) : this(size, false) { }
}

class Program
{
    static void Main()
    {
        var c = new Coffee("large");
        Console.WriteLine(c.Size + ", milk=" + c.Milk + ", sugar=" + c.Sugar);
    }
}

Validation in the Target

Because every constructor routes through the primary one, validation written there protects all creation paths.

using System;

class Rect
{
    public int W;
    public int H;

    public Rect(int w, int h)
    {
        if (w <= 0 || h <= 0)
            throw new ArgumentException("Dimensions must be positive");
        W = w;
        H = h;
    }

    public Rect(int side) : this(side, side) { }
}

class Program
{
    static void Main()
    {
        var square = new Rect(5);
        Console.WriteLine(square.W + "x" + square.H);
    }
}

Chaining vs Default Parameters

Default parameters can replace many overloads, but chaining is clearer when each constructor needs slightly different logic in its body.

using System;

class Logger
{
    public string Prefix;
    public bool Verbose;

    public Logger(string prefix, bool verbose)
    {
        Prefix = prefix;
        Verbose = verbose;
    }

    public Logger(string prefix) : this(prefix, false)
    {
        Console.WriteLine("Created quiet logger");
    }
}

class Program
{
    static void Main()
    {
        var log = new Logger("APP");
        Console.WriteLine(log.Prefix + " verbose=" + log.Verbose);
    }
}

Empty Bodies Are Common

A chaining constructor often has an empty { } body because all the work happens in the target.

using System;

class Vector
{
    public double X, Y, Z;

    public Vector(double x, double y, double z)
    {
        X = x; Y = y; Z = z;
    }

    public Vector(double v) : this(v, v, v) { }
}

class Program
{
    static void Main()
    {
        var v = new Vector(2);
        Console.WriteLine(v.X + "," + v.Y + "," + v.Z);
    }
}

Avoiding Cycles

Constructors must not chain in a loop (A calls B, B calls A). The compiler rejects circular : this(...) chains.

using System;

class Safe
{
    public int A, B;

    public Safe(int a, int b)
    {
        A = a;
        B = b;
    }

    public Safe(int a) : this(a, 0) { }   // chains one direction only
}

class Program
{
    static void Main()
    {
        var s = new Safe(7);
        Console.WriteLine(s.A + ", " + s.B);
    }
}

Combining with this.field

You can chain and still use this. in the body for any extra setup after the target constructor returns.

using System;

class Account
{
    public string Owner;
    public decimal Balance;
    public bool Active;

    public Account(string owner, decimal balance)
    {
        this.Owner = owner;
        this.Balance = balance;
    }

    public Account(string owner) : this(owner, 0m)
    {
        this.Active = true;
    }
}

class Program
{
    static void Main()
    {
        var a = new Account("Rin");
        Console.WriteLine(a.Owner + " active=" + a.Active);
    }
}

Putting It Together

Chaining keeps a class DRY: one constructor validates and assigns; the rest provide convenient shortcuts.

using System;

class Pizza
{
    public string Size;
    public int Toppings;

    public Pizza(string size, int toppings)
    {
        if (toppings < 0) throw new ArgumentException("toppings");
        Size = size;
        Toppings = toppings;
    }

    public Pizza(string size) : this(size, 0) { }
    public Pizza() : this("medium") { }
}

class Program
{
    static void Main()
    {
        var p = new Pizza();
        Console.WriteLine(p.Size + " with " + p.Toppings + " toppings");
    }
}

Quick Check

Test your understanding of constructor chaining.

Recap

Constructor chaining uses : this(args) to call another constructor in the same class. The target runs first, then the current body. Route every constructor through one primary constructor to centralize validation and avoid duplicated setup. Circular chains are not allowed.

using System;

class Demo
{
    public int X, Y;
    public Demo(int x, int y) { X = x; Y = y; }
    public Demo(int both) : this(both, both) { }
}

class Program
{
    static void Main()
    {
        var d = new Demo(4);
        Console.WriteLine(d.X + "," + d.Y);
    }
}

Frequently asked questions

Is the “Constructor Chaining with this” lesson free?

Yes — the full text of “Constructor Chaining with this” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Constructor Chaining with this”?

Reuse initialization across constructors. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Constructor Chaining with this” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Constructors
  2. Constructor Chaining with this
  3. Object and Collection Initializers
  4. Static Constructors
← Back to C# Academy