C# Academy · Lesson

Primary Constructors on Classes

Declare constructor parameters in the type header.

Lesson 1 of 413 steps

Primary Constructors on Classes is a free C# Academy lesson on CoddyKit. This is lesson 1 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, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

What Is a Primary Constructor?

A primary constructor lets you declare constructor parameters directly in the class header, like class Point(int x, int y). The parameters are in scope throughout the whole class body.

using System;

class Point(int x, int y) {
    public int X => x;
    public int Y => y;
}

var p = new Point(3, 4);
Console.WriteLine(p.X + "," + p.Y); // 3,4

No More Boilerplate

Before primary constructors you wrote a constructor body just to copy parameters into fields. Now the header declares the parameters and you reference them directly.

using System;

class Greeter(string name) {
    public string Greet() => "Hello, " + name + "!";
}

Console.WriteLine(new Greeter("Ada").Greet()); // Hello, Ada!

Parameters Are In Scope Everywhere

Primary constructor parameters can be used in property initializers, method bodies, and field initializers anywhere in the class.

using System;

class Circle(double radius) {
    public double Area => Math.PI * radius * radius;
    public double Circumference => 2 * Math.PI * radius;
}

var c = new Circle(2);
Console.WriteLine(Math.Round(c.Area, 2)); // 12.57

Initializing Fields and Properties

You can use a primary constructor parameter to initialize a field or property at declaration time.

using System;

class Account(decimal initial) {
    public decimal Balance { get; private set; } = initial;
    public void Deposit(decimal amount) => Balance += amount;
}

var a = new Account(100m);
a.Deposit(50m);
Console.WriteLine(a.Balance); // 150

Validation in the Header

You can validate primary constructor parameters inline using throw expressions when assigning to a field.

using System;

class Person(string name) {
    public string Name { get; } =
        string.IsNullOrWhiteSpace(name)
            ? throw new ArgumentException("name required")
            : name;
}

Console.WriteLine(new Person("Lin").Name); // Lin

Adding Extra Constructors

A class with a primary constructor can still declare additional constructors, but they must chain to the primary one with : this(...).

using System;

class Box(int width, int height) {
    public int Width => width;
    public int Height => height;
    public Box(int size) : this(size, size) { }
}

var sq = new Box(5);
Console.WriteLine(sq.Width + "x" + sq.Height); // 5x5

Primary Constructor Bodies

Primary constructors do not have a body of their own. Logic that must run at construction goes into field initializers or a chained regular constructor.

using System;

class Logger(string prefix) {
    private readonly string tag = "[" + prefix + "]";
    public void Log(string msg) => Console.WriteLine(tag + " " + msg);
}

new Logger("INFO").Log("started"); // [INFO] started

Not Automatically Public Properties

Unlike records, a primary constructor on a plain class does not create public properties. The parameters are private to the class unless you expose them yourself.

using System;

class Temp(double celsius) {
    // celsius is NOT a public property here
    public double Fahrenheit => celsius * 9 / 5 + 32;
}

Console.WriteLine(new Temp(100).Fahrenheit); // 212

Captured Parameters Become Fields

When a primary constructor parameter is used in a method (not just an initializer), the compiler captures it into a hidden field so it survives after construction.

using System;

class Counter(int start) {
    private int value = start;
    public int Next() => value++;
}

var c = new Counter(10);
Console.WriteLine(c.Next()); // 10
Console.WriteLine(c.Next()); // 11

Cleaner Dependency Injection

Primary constructors are popular for DI: declare services in the header and use them in methods, with no manual field assignment.

using System;

interface IClock { string Now(); }
class FakeClock : IClock { public string Now() => "12:00"; }

class Service(IClock clock) {
    public string Report() => "Time is " + clock.Now();
}

Console.WriteLine(new Service(new FakeClock()).Report()); // Time is 12:00

Putting It Together

This class uses a primary constructor for its dependencies and exposes a computed property, with no explicit constructor body.

using System;

class Rectangle(double w, double h) {
    public double Area => w * h;
    public double Perimeter => 2 * (w + h);
}

var r = new Rectangle(3, 4);
Console.WriteLine(r.Area + " " + r.Perimeter); // 12 14

Quick Check

Test your understanding of primary constructors on classes.

Recap

You learned primary constructors on classes.

  • class C(int x) declares constructor parameters in the header.
  • Parameters are in scope across the class body.
  • Extra constructors chain with : this(...).
  • On a plain class they are not public properties automatically.
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Primary Constructors on Classes” lesson free?

Yes — the full text of “Primary Constructors on Classes” is free to read here on the web. 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. The C# Academy course includes 4 lessons in total.

What will I learn in “Primary Constructors on Classes”?

Declare constructor parameters in the type header. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Primary Constructors on Classes” 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. Primary Constructors on Classes
  2. Capturing Parameters in Members
  3. Primary Constructors with Structs
  4. Combining with Properties and Bases
← Back to C# Academy