Whetstone.
Types: value vs referencevar, target-typed new, and init
Module 3 · Lesson 29 min

var, target-typed new, and init

Three small conveniences you’ll read constantly. var infers the type on the left from the right, like TS const without an annotation; the team uses it freely where the type is obvious from the initializer. Target-typed new goes the other way: when the type is already stated, you can write new() and let the compiler fill it in. And init accessors make a property settable only during object construction, which is how you get immutability without a giant constructor.

Reading real C# idiom
// var: type inferred from the right
var rows = await query.ToListAsync();      // List<FlightDisruptionCostCalc>

// target-typed new: type stated on the left, so new() is enough
private readonly List<EjError> _errors = new();

// init-only property: set at construction, immutable after
public record DecidedSnapshot
{
    public string FlightNumber { get; init; }
}
Practice

Try it yourself

Recall

What init buys you

Small idea, stated precisely.

What does an init accessor let you do that a set accessor does not, and why does the domain use it?

Reveal answer

init allows the property to be assigned during object construction (including object initializers) but not afterward, giving immutability without a hand-written constructor. The domain uses it so DTOs and value carriers can't be mutated after they're built.

Sign in to track your progress →