Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Constructors and initialization

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 35 of 85

Constructors establish invariants at birth; overloads and object initializers shape how callers build objects.

Constructors establish invariants

A constructor runs when new creates the object. Its job is to leave the object in a valid state โ€” reject nonsense immediately, never store it:

class Ticket
{
    public string Event;
    public int Price;

    public Ticket(string event_, int price)
    {
        Event = event_;
        if (price < 0) throw new ArgumentException("negative price");
        Price = price;
    }
}

var t = new Ticket("Concert", 250);   // valid at birth

The constructor's name matches the class and has no return type. Declaring any constructor removes the invisible parameterless one โ€” new Ticket() no longer compiles unless you write it yourself.

Overloads and initializers

Several constructors give callers choices, and one can chain another with this(...) so the validation lives in exactly one place:

public Ticket(string event_) : this(event_, 0) { }   // free tickets, same rules

For simple flat setup, an object initializer sets fields/properties right after new:

var t2 = new Ticket { Event = "Play", Price = 180 };

The initializer is sugar for "construct, then assign". It cannot validate โ€” if bad state must be rejected, put that in the constructor.