C# Academy · Lesson

Capturing Parameters in Members

Use primary constructor parameters throughout the type.

Lesson 2 of 413 steps

Capturing Parameters in Members is a free C# Academy lesson on CoddyKit. This is lesson 2 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.

Using Parameters in Members

The power of primary constructors comes from using the parameters inside members: methods, properties, and field initializers. The compiler arranges storage as needed.

using System;

class Multiplier(int factor) {
    public int Apply(int n) => n * factor;
}

Console.WriteLine(new Multiplier(3).Apply(5)); // 15

Capture vs Initializer-Only

If a parameter is used only in a field/property initializer, no extra storage is needed. If it is used in a method, the compiler captures it into a hidden field.

using System;

class A(int x) {
    public int Stored = x;     // initializer only
}
class B(int x) {
    public int Get() => x;     // captured into a field
}

Console.WriteLine(new A(7).Stored); // 7
Console.WriteLine(new B(9).Get());  // 9

Parameters Are Mutable

Primary constructor parameters behave like ordinary parameters: they are mutable within the instance unless you treat them as read-only. Mutating one changes the captured field.

using System;

class Toggle(bool on) {
    public void Flip() => on = !on;
    public bool State => on;
}

var t = new Toggle(false);
t.Flip();
Console.WriteLine(t.State); // True

Exposing as Read-Only Properties

A common pattern is to expose a parameter through a get-only property, giving a clean public surface while keeping the parameter internal.

using System;

class User(string email) {
    public string Email { get; } = email;
}

Console.WriteLine(new User("a@b.com").Email); // a@b.com

Computed Properties from Parameters

Parameters feed naturally into expression-bodied computed properties.

using System;

class Money(decimal amount, string currency) {
    public string Display => amount.ToString("0.00") + " " + currency;
}

Console.WriteLine(new Money(9.5m, "USD").Display); // 9.50 USD

Parameters in Multiple Methods

The same captured parameter can be shared by many methods, acting like a private field initialized at construction.

using System;

class Range2(int lo, int hi) {
    public bool Contains(int n) => n >= lo && n <= hi;
    public int Clamp(int n) => Math.Max(lo, Math.Min(hi, n));
}

var r = new Range2(0, 10);
Console.WriteLine(r.Contains(5)); // True
Console.WriteLine(r.Clamp(15));   // 10

Capturing Reference Types

Captured reference-type parameters let you store injected dependencies and call them from any method.

using System;
using System.Collections.Generic;

class Repo(List<string> store) {
    public void Add(string item) => store.Add(item);
    public int Count => store.Count;
}

var list = new List<string>();
var repo = new Repo(list);
repo.Add("x");
Console.WriteLine(repo.Count); // 1

Avoiding Accidental Double Storage

If you both assign a parameter to a field and use the parameter in methods, you may end up storing the value twice. Prefer one approach for clarity.

using System;

class Clean(int x) {
    // expose once via property, reference property in methods
    public int Value { get; } = x;
    public int Doubled() => Value * 2;
}

Console.WriteLine(new Clean(4).Doubled()); // 8

Parameters in Lambdas

Parameters can be captured by lambdas defined inside the class, just like fields.

using System;
using System.Collections.Generic;
using System.Linq;

class Filter(int threshold) {
    public IEnumerable<int> KeepAbove(IEnumerable<int> xs) =>
        xs.Where(n => n > threshold);
}

var f = new Filter(3);
Console.WriteLine(string.Join(",", f.KeepAbove(new[]{1,2,3,4,5}))); // 4,5

Scope Lifetime

A captured parameter lives as long as the object. It is effectively instance state, so do not assume it is reset between method calls.

using System;

class Accumulator(int seed) {
    private int total = seed;
    public int Add(int n) { total += n; return total; }
}

var a = new Accumulator(100);
Console.WriteLine(a.Add(1)); // 101
Console.WriteLine(a.Add(1)); // 102

Putting It Together

This service captures two dependencies and uses them across methods, showing the typical real-world shape.

using System;

class PriceService(decimal taxRate, string currency) {
    public decimal WithTax(decimal net) => net * (1 + taxRate);
    public string Format(decimal v) => v.ToString("0.00") + " " + currency;
}

var s = new PriceService(0.2m, "EUR");
Console.WriteLine(s.Format(s.WithTax(100m))); // 120.00 EUR

Quick Check

Confirm how primary constructor parameters are stored.

Recap

You learned how primary constructor parameters are used in members.

  • Used in methods, parameters are captured into hidden fields.
  • Used only in initializers, no extra storage is needed.
  • Expose them via get-only properties for a clean surface.
  • They behave as instance state for the object lifetime.
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 “Capturing Parameters in Members” lesson free?

Yes — the full text of “Capturing Parameters in Members” 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 “Capturing Parameters in Members”?

Use primary constructor parameters throughout the type. 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 2 of 4.

How long does the “Capturing Parameters in Members” 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