C# Academy · Lesson

Primary Constructors with Structs

Apply primary constructors to value types.

Lesson 3 of 413 steps

Primary Constructors with Structs is a free C# Academy lesson on CoddyKit. This is lesson 3 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.

Primary Constructors on Structs

Structs support primary constructors too: struct Point(int x, int y). They work much like on classes, declaring parameters in the header.

using System;

struct 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

A Parameterless Constructor Still Exists

Structs always have an implicit parameterless constructor producing the default value. A primary constructor adds a parameterized one alongside it.

using System;

struct Vec(int x, int y) {
    public int X => x;
    public int Y => y;
}

var zero = new Vec(); // default: x=0, y=0
Console.WriteLine(zero.X + "," + zero.Y); // 0,0

Exposing Parameters via Properties

As with classes, struct primary constructor parameters are not public properties automatically; expose them explicitly.

using System;

struct Size(int w, int h) {
    public int Width { get; } = w;
    public int Height { get; } = h;
}

var s = new Size(10, 20);
Console.WriteLine(s.Width * s.Height); // 200

Computed Members

Struct primary constructors pair nicely with computed, expression-bodied members.

using System;

struct Complex(double re, double im) {
    public double Magnitude => Math.Sqrt(re * re + im * im);
}

Console.WriteLine(new Complex(3, 4).Magnitude); // 5

Readonly Structs

A readonly struct with a primary constructor guarantees immutability. All members must not mutate state, which fits get-only properties perfectly.

using System;

readonly struct Money(decimal amount, string currency) {
    public decimal Amount { get; } = amount;
    public string Currency { get; } = currency;
    public string Display => Amount.ToString("0.00") + " " + Currency;
}

Console.WriteLine(new Money(5m, "USD").Display); // 5.00 USD

Chaining to the Primary Constructor

Additional struct constructors must call the primary one with : this(...), just like classes.

using System;

struct Square(int side) {
    public int Side => side;
    public int Area => side * side;
    public Square() : this(1) { } // explicit default side
}

Console.WriteLine(new Square(4).Area); // 16

Default vs Primary Constructor

Note that new Square() can use either the implicit zero-init or your declared parameterless constructor. If you define one that chains, it runs your logic.

using System;

struct Pct(double value) {
    public double Value { get; } = value;
    public Pct() : this(100) { }
}

Console.WriteLine(new Pct().Value); // 100

Value Semantics Remain

Structs still copy by value. A primary constructor does not change that: assigning a struct copies all its fields.

using System;

struct Counter(int n) {
    public int N { get; set; } = n;
}

var a = new Counter(1);
var b = a;       // copy
b.N = 99;
Console.WriteLine(a.N + " " + b.N); // 1 99

Initializing Mutable Fields

You can initialize settable properties or fields from parameters, though immutable structs are usually preferable.

using System;

struct Bag(int capacity) {
    public int Capacity { get; set; } = capacity;
    public int Used { get; set; } = 0;
}

var bag = new Bag(10);
bag.Used = 3;
Console.WriteLine(bag.Capacity - bag.Used); // 7

Deconstruction

Pair a primary constructor with a Deconstruct method to get tuple-like unpacking.

using System;

struct Point(int x, int y) {
    public int X => x;
    public int Y => y;
    public void Deconstruct(out int a, out int b) { a = x; b = y; }
}

var (px, py) = new Point(7, 8);
Console.WriteLine(px + "," + py); // 7,8

Putting It Together

A small immutable value type built with a readonly struct and a primary constructor.

using System;

readonly struct Temperature(double celsius) {
    public double Celsius { get; } = celsius;
    public double Fahrenheit => Celsius * 9 / 5 + 32;
}

var t = new Temperature(25);
Console.WriteLine(t.Fahrenheit); // 77

Quick Check

Test your grasp of primary constructors with structs.

Recap

You learned primary constructors on structs.

  • struct P(int x, int y) declares parameters in the header.
  • The implicit parameterless constructor still exists.
  • Combine with readonly struct for immutable value types.
  • Extra constructors chain with : this(...).
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 with Structs” lesson free?

Yes — the full text of “Primary Constructors with Structs” 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 with Structs”?

Apply primary constructors to value types. 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 3 of 4.

How long does the “Primary Constructors with Structs” 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