Skip to content
Codewilds

Lesson 20 of 34 · Classes & Objects

C# Constructors: Overloading, Chaining and Primary Constructors

medium20 XP3 min read

An object should be usable the moment it exists. With object initializers alone, nothing stops someone from creating a potion with no name or a negative power. A constructor is a special method that runs when an object is created with new. It receives the starting values, validates them and puts the object into a valid state — once, in one place.

Writing a constructor

A constructor has the same name as the class and no return type:

public class Weapon
{
    public string Name { get; }
    public int Damage { get; }

    public Weapon(string name, int damage)
    {
        Name = name;
        Damage = damage < 0 ? 0 : damage;
    }
}

var axe = new Weapon("Axe", 12);
Console.WriteLine($"{axe.Name}: {axe.Damage}");   // Axe: 12

Name and Damage are get-only properties: they can be set in the constructor and never again. Objects that cannot change after creation are easier to reason about.

If a class declares no constructor at all, C# gives it an invisible parameterless one — that is why new Hero() worked in the earlier lessons. As soon as you write your own constructor, that free one disappears.

Overloading and chaining constructors

Like methods, constructors can be overloaded. To avoid repeating the setup code, one constructor can call another with : this(...):

public class Weapon
{
    public string Name { get; }
    public int Damage { get; }

    public Weapon(string name, int damage)
    {
        Name = name;
        Damage = Math.Max(0, damage);
    }

    public Weapon(string name) : this(name, 5) { }   // default damage
    public Weapon() : this("Fists") { }               // chains twice
}

All the rules live in one constructor; the others just supply defaults.

Constructors and object initializers

Constructors and initializers work together: the constructor runs first, then the initializer sets any remaining settable properties.

public class Quest
{
    public string Title { get; }
    public int Reward { get; set; } = 10;

    public Quest(string title) => Title = title;
}

var q = new Quest("Slay the rat") { Reward = 50 };

Primary constructors

C# 12 added primary constructors: the parameters are declared right after the class name and are available in the whole class body. They are great for small classes:

public class Spell(string name, int manaCost)
{
    public string Name { get; } = name;
    public int ManaCost { get; } = manaCost;
    public bool CanCast(int mana) => mana >= manaCost;
}

var bolt = new Spell("Bolt", 8);
Console.WriteLine(bolt.CanCast(5));   // False

Common mistakes

  • Adding a return type: public void Weapon(...) is an ordinary method, not a constructor.
  • Forgetting to assign a property: a get-only string property you never set stays null.
  • Copy-pasted constructors: when two constructors repeat the same logic, chain them with this(...).

Assignment

Complete two classes:

  1. Potion has get-only properties Name and Power and three constructors:
    • Potion(name, power) stores the name and clamps the power to the range 0–100;
    • Potion(name) creates a potion with power 10;
    • Potion() creates "Water" with power 0.
  2. potion.Mix(other) returns a new potion named "<Name> + <other.Name>" whose power is the sum of both (still clamped). The original potions do not change.
  3. Rune uses a primary constructor Rune(char symbol, int strength): expose them as get-only properties Symbol and Strength, and make Describe() return "Rune F (7)" for new Rune('F', 7).

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The C# cheat sheet has every topic of Learn C# on one page.

Lesson last updated on .

Write code ↓
C#+20 XP

Lesson 20 is part of the full course. The first 8 lessons are free.

Premium lesson

Unlock the full Learn C# course

  • All 34 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.