Combining with Properties and Bases
Forward parameters to base types and properties.
Combining with Properties and Bases is a free C# Academy lesson on CoddyKit — lesson 4 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.
Combining Primary Constructors with Inheritance
A derived class with a primary constructor can forward its parameters to a base class using the : Base(args) syntax in the header.
using System;
class Animal(string name) {
public string Name => name;
}
class Dog(string name) : Animal(name) {
public string Speak() => Name + " says Woof";
}
Console.WriteLine(new Dog("Rex").Speak()); // Rex says WoofForwarding a Subset of Parameters
The derived class can take more parameters than the base and pass only the relevant ones upward.
using System;
class Shape(string color) {
public string Color => color;
}
class Box(string color, int size) : Shape(color) {
public int Size => size;
}
var b = new Box("red", 5);
Console.WriteLine(b.Color + " " + b.Size); // red 5Transforming Arguments to the Base
You can compute base-constructor arguments from the derived parameters inline in the header.
using System;
class Label(string text) {
public string Text => text;
}
class UpperLabel(string text) : Label(text.ToUpper()) {
}
Console.WriteLine(new UpperLabel("hi").Text); // HIAdding Properties Alongside
A primary constructor and explicit properties coexist. Initialize properties from the parameters at declaration.
using System;
class Product(string name, decimal price) {
public string Name { get; } = name;
public decimal Price { get; set; } = price;
}
var p = new Product("Book", 12m);
p.Price = 10m;
Console.WriteLine(p.Name + " " + p.Price); // Book 10Base Parameters and Derived Use
Even after forwarding to the base, a derived primary constructor parameter can still be used in the derived class body.
using System;
class Base(int id) {
public int Id => id;
}
class Item(int id, string label) : Base(id) {
public string Describe() => "#" + id + ": " + label;
}
Console.WriteLine(new Item(7, "Widget").Describe()); // #7: WidgetRecords and Primary Constructors
Records use primary constructors to generate public init-only properties automatically. A positional record is the most concise case.
using System;
record Person(string First, string Last) {
public string Full => First + " " + Last;
}
Console.WriteLine(new Person("Ada", "Lovelace").Full); // Ada LovelaceRecord Inheritance
Derived records forward to a base record positionally, generating the combined property set.
using System;
record Vehicle(string Make);
record Car(string Make, int Doors) : Vehicle(Make);
var c = new Car("Toyota", 4);
Console.WriteLine(c.Make + " " + c.Doors); // Toyota 4Validation Before Base Call
If you need validation, do it in a property initializer using a throw expression; the base call still runs first.
using System;
class Base(string tag) { public string Tag => tag; }
class Node(string tag) : Base(tag) {
public string Tag2 { get; } =
tag.Length > 0 ? tag : throw new ArgumentException("empty");
}
Console.WriteLine(new Node("a").Tag); // aMixing Captured Params and Properties
You can keep some parameters as captured private state and expose others as properties, tailoring the public surface.
using System;
class Order(int id, decimal total) {
public decimal Total { get; } = total; // public
public string Reference() => "ORD-" + id; // id stays private
}
var o = new Order(42, 99m);
Console.WriteLine(o.Reference() + " " + o.Total); // ORD-42 99Default Property Values
Properties not tied to parameters can still get default initializers, combining freely with primary constructor parameters.
using System;
class Session(string user) {
public string User { get; } = user;
public bool Active { get; set; } = true;
}
var s = new Session("kai");
Console.WriteLine(s.User + " " + s.Active); // kai TruePutting It Together
A derived class that forwards to a base, adds a property, and exposes a computed member.
using System;
class Employee(string name) {
public string Name => name;
}
class Manager(string name, int teamSize) : Employee(name) {
public int TeamSize { get; } = teamSize;
public string Title => Name + " (manages " + TeamSize + ")";
}
Console.WriteLine(new Manager("Sam", 5).Title); // Sam (manages 5)Quick Check
Confirm how primary constructors forward to base classes.
Recap
You learned to combine primary constructors with properties and base classes.
class D(...) : Base(args)forwards to the base.- Arguments to the base can be computed inline.
- Properties can be initialized from parameters at declaration.
- Records generate public init-only properties from positional parameters.
Frequently asked questions
Is the “Combining with Properties and Bases” lesson free?
Yes — the full text of “Combining with Properties and Bases” 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 “Combining with Properties and Bases”?
Forward parameters to base types and properties. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Combining with Properties and Bases” 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
- Primary Constructors on Classes
- Capturing Parameters in Members
- Primary Constructors with Structs
- Combining with Properties and Bases