×

c# · level 2 · unit 4 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

copies a record and changes only what you name.

A record cannot change, but you often want one that is nearly the same. The word with copies a record and changes only the properties you name: monday with { Degrees = 17 }. Everything else is copied across as it was.

The original is left alone, so you can keep both. That makes with handy for a history, where each day's weather is its own record. Change several properties at once by separating them with commas inside the curly brackets.

using System;

var monday = new Weather("Leeds", 14, "cloudy");
var tuesday = monday with { Degrees = 17 };
var wednesday = tuesday with { Degrees = 12, Sky = "rainy" };
Console.WriteLine(monday);
Console.WriteLine(tuesday);
Console.WriteLine(wednesday);

record Weather(string Town, int Degrees, string Sky);

Monday still says 14 and cloudy at the end, because nothing ever changed it.